61
OptisWorks Automation 2014 SP1

OptisWorks Automation 2014 SP1 - Optis Portal

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OptisWorks Automation 2014 SP1 - Optis Portal

OptisWorks

Automation

2014 SP1

Page 2: OptisWorks Automation 2014 SP1 - Optis Portal
Page 3: OptisWorks Automation 2014 SP1 - Optis Portal

Table of Contents

What is Automation .................................................................................................. 4

Automation in OptisWorks .......................................................................................... 5

APIs ...................................................................................................................... 6 OptisWorks API Index ......................................................................................... 6 Calling OptisWorks APIs....................................................................................... 6 OptisWorks3DTextures Object .............................................................................. 8

Method ................................................................................................... 8 OptisWorksAmbientSources Object ......................................................................... 9

Methods.................................................................................................. 9 Example ................................................................................................. 9

OptisWorks Ambient Source Interfaces ................................................................... 14 IDateTimeObjectSource Inteface .................................................................. 14 IEnvironment Interface .............................................................................. 14 ILatitudeObjectSource Interface ................................................................... 15 ILongitudeObjectSource Interface ................................................................. 16 INaturalLight Interface .............................................................................. 17 IStandardGeneralSky interface ..................................................................... 17 IStandardOvercastSky Interface .................................................................... 18 IUniform Interface .................................................................................... 19

OptisWorksCatalog Object .................................................................................. 20 Methods................................................................................................. 20

OptisWorksDetectors Object ............................................................................... 21 Methods................................................................................................. 21

OptisWorks Detector Interfaces ............................................................................ 22 IIlluminanceDetector Interface .................................................................... 22 IIntensityDetector Interface ........................................................................ 22 ILuminanceDetector Interface...................................................................... 22 IPolarIntensityDetector Interface .................................................................. 23 Example ................................................................................................ 24

OptisWorksDirectSimulation Object ....................................................................... 26 Methods................................................................................................. 26 Example ................................................................................................ 28

OptisWorksFaces Object ..................................................................................... 31 Methods................................................................................................. 31 Example ................................................................................................ 32

OptisWorksInstance Object ................................................................................. 33 Methods................................................................................................. 34

OptisWorksInverseSimulation Object ..................................................................... 38 Methods................................................................................................. 38 Example ................................................................................................ 39

OptisWorksPartPreferences Object ....................................................................... 41 Methods................................................................................................. 41 Example ................................................................................................ 41

OptisWorksSurfaceSource Object .......................................................................... 43 Properties .............................................................................................. 43 Example ................................................................................................ 44

Tutorial ............................................................................................................... 46 First Macro Lesson 1: Recording Events .................................................................. 46 First Macro Lesson 2: Modifying an Existing Macro ..................................................... 48 First Macro Lesson 3: Running the Macro ................................................................. 50 Second Macro Lesson 4: Creating a Second Macro from Scratches .................................. 50 Second Macro Lesson 5: Updating the Macro ............................................................ 51 Second Macro Lesson 6: Presentation of the Results ................................................... 54

First Macro Script ................................................................................................... 56

Second Macro Script ............................................................................................... 58

Page 4: OptisWorks Automation 2014 SP1 - Optis Portal

Page 4 of 61 OptisWorks Automation User Guide

WHAT IS AUTOMATION

General Description

With Automation, you can communicate with any Windows applications.

Automation can replace the old specific macro way method for writing script.

With automation you can:

Write script using any language compatible with Automation (VB Script, C++...),

Directly access results data without knowing specific format (like XMP file),

Write your own HTML reports that automatically analyze the results,

Write your own optimization code...

Technical Description

Automation is based on COM (Component Object Model).

This is a standard way to describe the interface exposed by a Windows application to other Windows applications.

The interface is described using the IDL (Interface Description Language).

This interface is used to give the description of all data and functions (and their parameters) that can be called from

another application.

The exposed data are called Properties.

The exposed functions are called Methods.

Development Tools

If you want to use Automation, you need to decide which language to use depending on your needs.

For basic needs the VB Script or VBA language is the easiest to understand and to use for a non-developer.

You can use VB Script in HTML file.

You can use FrontPage for editing the HTML file.

VB Script or VBA is also included in a lot of software.

When you write a macro with this software, the VB Script language is used.

So it is very easy to write macro that mix CAD VB Script or VBA instructions and Virtual Photometric Lab VB Script

instructions : using your own merit function you have your own optimization tool.

For more complex needs, you must use C++, which is a more difficult language to learn for non developers.

If you want to have advices, code fraction and API functions' examples, you can view these APIs

(http://www.solidworks.com/api).

Page 5: OptisWorks Automation 2014 SP1 - Optis Portal

Automation in OptisWorks Page 5 of 61

AUTOMATION IN OPTISWORKS

What You Can Do with Automation in OptisWorks

You can launch simulations, get the results and load them.

OptisWorks Automation includes functions giving access to data.

This section of the OptisWorks help includes:

A Tutorial (see page 46) section that gives an overview of what OptisWorks Automation can achieve with two

examples of Visual Basic script

The description of all the OptisWorks APIs (see page 6).

All examples are given using VB Script language but any other language compatible with Automation can be used.

What OptisWorks APIs Can Achieve

Changing sources' configuration (for example flux, type of spectrum...)

Changing the part preferences (internal/external materials, surface quality)

Launching illuminance and luminance simulations (Direct and Inverse simulations)

OptisWorks APIs can be used with OptisLabs Automation to analyze the results (you can view OptisLabs Automation

Index or OptisLabs OnLine Help).

Using OptisWorks Add-In or OptisWorks Studio

In order to modify the properties of a part or to launch simulations, the scrip needs to access first to the SolidWorks

application to then modify the active document (part or assembly).

However, the access to the SolidWorks application is not the same according to the version of OptisWorks used.

The two different codes are presented below.

For OptisWorks Add-In

Dim swApp As Object

Set swApp = Application.SldWorks

For OptisWorks Studio

Dim swApp As Object

Set swApp = GetObject(Class:="OptisWorksStudio.Application")

Page 6: OptisWorks Automation 2014 SP1 - Optis Portal

Page 6 of 61 OptisWorks Automation User Guide

APIS

OptisWorks API Index

OptisWorks API Object

OptisWorks3DTextures (see page 8)

OptisWorksAmbientSources (see page 9)

OptisWorksCatalog (see page 20)

OptisWorksDetectors (see page 21)

OptisWorksFaces (see page 31)

OptisWorksDirectSimulation (see page 26)

OptisWorksInstance (see page 33)

OptisWorksInverseSimulation (see page 38)

OptisWorksPartPreferences (see page 41)

OptisWorksSurfaceSource (see page 43)

OptisWorks Interfaces

IDateTimeObjectSource (see page 14)

IEnvironment (see page 14)

IIlluminanceDetector (see page 22)

IIntensityDetector (see page 22)

ILatitudeObjectSource (see page 15)

ILongitudeObjectSource (see page 16)

ILuminanceDetector (see page 22)

INaturalLight (see page 17)

IPolarIntensityDetector (see page 23)

IStandardGeneralSky (see page 17)

IStandardOvercastSky (see page 18)

IUniform (see page 19)

OptisLabs APIs

OptisLabs Automation Index

Calling OptisWorks APIs

1. To check the references in Microsoft Visual Basic to execute your script, in this program, click Tools,

References.

A box appears.

2. Select OptisWorks Catalog 1.0 Type Library and OptisWorks script 1.0 Type Library.

3. If these references are not in the list, do the following:

1. Click Browse.

2. In the OptisWorks 20XX folder, itself in Optis, select OptisWorksCatalog.dll.

It corresponds to the reference OptisWorks Catalog 1.0 Type Library.

3. If you have a 32 bits operating system version, select also OptisWorks.dll.

Page 7: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 7 of 61

4. If you have a 64 bits operating system version, select also OptisWorks.tlb in the OW64 folder.

They correspond to the reference OptisWorks script 1.0 Type Library.

4. Before using any OptisWorks methods, use the following instruction:

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

This instruction automatically runs Catalog to create an instance of the application.

The APIs must refer to the variable Catalog.

The following example shows the use of Catalog to create the instance (Instance) referring to the active

document of SolidWorks (ModelDoc).

Set Instance = Catalog.Find(ModelDoc)

Set PartPreferences=Instance.GetPartPreferences()

The object PartPreferences is then defined using Instance as been a GetPartPreferences object.

There is no reference to the location of Catalog on the disk.

It is because, as an Automation application, it has been registered in the Windows' registry, linking the

application we want to reach with OptisWorksCatalog.Catalog.

Page 8: OptisWorks Automation 2014 SP1 - Optis Portal

Page 8 of 61 OptisWorks Automation User Guide

OptisWorks3DTextures Object

OptisWorks3DTextures object is part of the OptisWorks library.

Method

ExportPattern Method

Description

It exports the active geometry as a 3D texture's pattern.

Syntax

object.ExportPattern(BSTR bstrFullPathFileName) as Boolean

object: OptisWorks3DTextures object

bstrFullPathFileName: full path of the pattern text file.

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim 3DTexture As OptisWorks3DTextures

Dim retval As Boolean

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("optisworkscatalog.catalog")

Set Instance = Catalog.Find(ModelDoc)

Set 3DTexture = Instance.Get3DTextures

retval = 3DTexture.ExportPattern("c:\MyPattern.txt")

Page 9: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 9 of 61

OptisWorksAmbientSources Object

OptisWorksAmbientSources object is part of the OptisWorks library.

Methods

GetAmbientSource Method

Description

It gets the ambient source.

It returns the type of ambient source between uniform (owAmbientSourceTypeUniform), CIE Overcast Sky

(owAmbientSourceTypeCieOvercastSky), CIE General Sky (owAmbientSourceTypeCieGeneralSky), Natural Light

(owAmbientSourceTypeNaturalLight) or Environment (owAmbientSourceTypeEnvironment).

Syntax

object. GetAmbientSource(strSourceName as string) As Long

object: OptisWorksAmbientSources object

strSourceName: Ambient source name (input).

Update Method

Description

Update the ambient source. Returns True if no error has occurred, False otherwise.

Syntax

object. Update(bForceUpdate as Boolean) As Boolean

object: OptisWorksAmbientSources object

bForceUpdate: option to force the ambient source update (optional input, False by default)

Example

Example OptisWorksAmbientSources

'---- Definitions for SolidWorks

Dim swApp As Object

Dim ModelDoc As ModelDoc2

'---- Definitions for OptisWorks

Dim OWCatalog As OptisWorksCatalog

Dim OWInstance As OptisWorksInstance

Dim AmbientSource As OptisWorksAmbientSources

Sub Main()

On Error GoTo ErrorHandler

Dim retval

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set OWCatalog = CreateObject("OptisWorksCatalog.Catalog")

Set OWInstance = OWCatalog.Find(ModelDoc)

Page 10: OptisWorks Automation 2014 SP1 - Optis Portal

Page 10 of 61 OptisWorks Automation User Guide

Set AmbientSource = OWInstance.GetAmbientSources

If AmbientSource.GetAmbientSource("Ambient source 1") = owAmbientSourceTypeUniform Then

Dim UniformSource As IUniform

Set UniformSource = AmbientSource

UniformSource.Luminance = 500

AmbientSource.Update

End If

If AmbientSource.GetAmbientSource("Ambient source 1") = owAmbientSourceTypeCieOvercastSky

Then

Dim CIEOvercast As IStandardOvercastSky

Set CIEOvercast = AmbientSource

CIEOvercast.Luminance = 1000

AmbientSource.Update

End If

If AmbientSource.GetAmbientSource("Ambient source 1") = owAmbientSourceTypeCieGeneralSky

Then

Dim CIEGeneral As IStandardGeneralSky

Set CIEGeneral = AmbientSource

MsgBox(CIEGeneral.Date)

Dim SourceDateTime As IDateTimeObjectSource

Set SourceDateTime = CIEGeneral.GetDateTimeObjectSource

retval = SourceDateTime.Day & vbTab & SourceDateTime.Month & vbTab & SourceDateTime.Year

& vbTab & SourceDateTime.Minute & vbTab & SourceDateTime.Hour

MsgBox (retval)

SourceDateTime.Day = 14

SourceDateTime.Month = 7

SourceDateTime.Year = 2010

SourceDateTime.Minute = 12

SourceDateTime.Hour = 12

Dim SourceLatitude As ILatitudeObjectSource

Set SourceLatitude = CIEGeneral.GetLatitudeObjectSource

retval = SourceLatitude.Degree & vbTab & SourceLatitude.Minute & vbTab &

SourceLatitude.Second

MsgBox (retval)

SourceLatitude.Degree = 14

Page 11: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 11 of 61

SourceLatitude.Minute = 7

SourceLatitude.Second = 1

Dim SourceLongitude As ILongitudeObjectSource

Set SourceLongitude = CIEGeneral.GetLongitudeObjectSource

retval = SourceLongitude.Degree & vbTab & SourceLongitude.Minute & vbTab &

SourceLongitude.Second

MsgBox (retval)

SourceLongitude.Degree = 28

SourceLongitude.Minute = 14

SourceLongitude.Second = 7

CIEGeneral.Luminance = 500

AmbientSource.Update

End If

If AmbientSource.GetAmbientSource("Ambient source 1") = owAmbientSourceTypeNaturalLight

Then

Dim NaturalLight As INaturalLight

Set NaturalLight = AmbientSource

MsgBox(NaturalLight.Date)

Set SourceDateTime = NaturalLight.GetDateTimeObjectSource

retval = SourceDateTime.Day & vbTab & SourceDateTime.Month & vbTab & SourceDateTime.Year

& vbTab & SourceDateTime.Minute & vbTab & SourceDateTime.Hour

MsgBox (retval)

SourceDateTime.Day = 14

SourceDateTime.Month = 7

SourceDateTime.Year = 2010

SourceDateTime.Minute = 12

SourceDateTime.Hour = 12

Set SourceLatitude = NaturalLight.GetLatitudeObjectSource

retval = SourceLatitude.Degree & vbTab & SourceLatitude.Minute & vbTab &

SourceLatitude.Second

MsgBox (retval)

SourceLatitude.Degree = 14

SourceLatitude.Minute = 7

SourceLatitude.Second = 1

Page 12: OptisWorks Automation 2014 SP1 - Optis Portal

Page 12 of 61 OptisWorks Automation User Guide

Set SourceLongitude = NaturalLight.GetLongitudeObjectSource

retval = SourceLongitude.Degree & vbTab & SourceLongitude.Minute & vbTab &

SourceLongitude.Second

MsgBox (retval)

SourceLongitude.Degree = 28

SourceLongitude.Minute = 14

SourceLongitude.Second = 7

NaturalLight.Turbidity = 8

AmbientSource.Update

End If

If AmbientSource.GetAmbientSource("Ambient source 1") = owAmbientSourceTypeEnvironment

Then

Dim Environment As IEnvironmentSource

Set Environment = AmbientSource

retval = Environment.RedSpectrum

retval = Environment.GreenSpectrum

retval = Environment.BlueSpectrum

retval = Environment.Luminance

retval = Environment.Environment

Environment.Luminance = 500

Environment.RedSpectrum = "C:\Program Files

(x86)\OPTIS\Library\Spectrum\Import_bitmap\Gaussian_red.spectrum"

Environment.GreenSpectrum = "C:\Program Files

(x86)\OPTIS\Library\Spectrum\Import_bitmap\Gaussian_green.spectrum"

Environment.BlueSpectrum = "C:\Program Files

(x86)\OPTIS\Library\Spectrum\Import_bitmap\Gaussian_blue.spectrum"

Environment.Environment = ""C:\MyFolder\DH206HC.exr"

AmbientSource.Update

End If

ErrorHandler:

If Err.Number <> 0 Then

strMsg = "Error number : " & Str(Err.Number) & " has been generated by " _

& Err.Source & Chr(13) & "Description : " & Err.Description

MsgBox strMsg, , "Erreur", Err.HelpFile, Err.HelpContext

End If

Set AmbientSource = Nothing

Set OWInstance = Nothing

Set OWCatalog = Nothing

Page 13: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 13 of 61

End Sub

Page 14: OptisWorks Automation 2014 SP1 - Optis Portal

Page 14 of 61 OptisWorks Automation User Guide

OptisWorks Ambient Source Interfaces

IDateTimeObjectSource Inteface

With the IDateTimeObjectSource, you can manage the date and time parameters for a CIE General Sky ambient

source and a Natural Light ambient source.

Properties

Day Property

Description

With it, you can get or set the day parameter of an IDateTimeObjectSource interface.

Syntax

object.Day() As Integer

object: IDateTimeObjectSource interface

Hour Property

Description

With it, you can get or set the hour parameter of an IDateTimeObjectSource interface.

Syntax

object.Hour() As Integer

object: IDateTimeObjectSource interface

Minute_time Property

Description

With it, you can get or set the minute parameter of an IDateTimeObjectSource interface.

Syntax

object.Minute() As Integer

object: IDateTimeObjectSource interface

Month Property

Description

With it, you can get or set the month parameter of an IDateTimeObjectSource interface.

Syntax

object.Month() As Integer

object: IDateTimeObjectSource interface

Year Property

Description

With it, you can get or set the year parameter of an IDateTimeObjectSource interface.

Syntax

object.Year() As Integer

object: IDateTimeObjectSource interface

IEnvironment Interface

Description

With the IEnvironment Interface, you can manage an Environment ambient source.

Properties

BlueSpectrum (see page 15)

Page 15: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 15 of 61

Environment (see page 15)

GreenSpectrum (see page 15)

Luminance (see page 15)

RedSpectrum (see page 15)

Properties

BlueSpectrum Property

Description

With it, you can get or set the blue spectrum file of an Environment ambient source.

Syntax

object. BlueSpectrum() As String

object: IEnvironment interface

Environment Property

Description

With it, you can get or set the environment file of an Environment ambient source.

Syntax

object. Environment() As String

object: IEnvironment interface

GreenSpectrum Property

Description

With it, you can get or set the green spectrum file of an Environment ambient source.

Syntax

object. GreenSpectrum() As String

object: IEnvironment interface

Luminance Property

Description

With it, you can get or set the luminance value of an ambient source.

Syntax

object. Luminance() As Double

object: IEnvironment, IStandardGeneralSky, IStandardOvercastSky or IUniform interface

RedSpectrum Property

Description

With it, you can get or set the red spectrum file of an Environment ambient source.

Syntax

object. RedSpectrum() As String

object: IEnvironment interface

ILatitudeObjectSource Interface

With the ILatitudeObjectSource Interface, you can manage latitude parameters for a CIE General Sky ambient source

and a Natural Light ambient source.

Page 16: OptisWorks Automation 2014 SP1 - Optis Portal

Page 16 of 61 OptisWorks Automation User Guide

Properties

Degree Property

Description

With it, you can get or set the degree parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Degree() As Long

object: ILongitudeObjectSource or ILatitudeObjectSource interface

Minute_angle Property

Description

With it, you can get or set the minute parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Minute() As Long

object: ILongitudeObjectSource or ILatitudeObjectSource interface

Second Property

Description

With it, you can get or set the second parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Second() As Long

object: ILongitudeObjectSource or ILatitudeObjectSource interface

ILongitudeObjectSource Interface

With the ILongitudeObjectSource Interface, ypyou can manage longitude parameters for a CIE General Sky ambient

source and a Natural Light ambient source.

Properties

Degree Property

Description

With it, you can get or set the degree parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Degree() As Long

object: ILongitudeObjectSource or ILatitudeObjectSource interface

Minute_angle Property

Description

With it, you can get or set the minute parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Minute() As Long

object: ILongitudeObjectSource or ILatitudeObjectSource interface

Second Property

Description

With it, you can get or set the second parameter of an ILongitudeObjectSource or ILatitudeObjectSource interface.

Syntax

object. Second() As Long

Page 17: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 17 of 61

object: ILongitudeObjectSource or ILatitudeObjectSource interface

INaturalLight Interface

With the INaturalLight Interface, you can manage a Natural Light ambient source.

Methods

GetDateTimeObjectSource Method

Description

It returns an IDateTimeObjectSource interface allowing getting or setting the date and time parameters of an

ambient source.

Syntax

object. GetDateTimeObjectSource() As IDateTimeObjectSource

object: INaturalLight or IStandardGeneralSky interface

GetLatitudeObjectSource Method

Description

It returns an ILatitudeObjectSource interface allowing getting or setting the latitude parameters of an ambient

source.

Syntax

object. GetLatitudeObjectSource() As ILatitudeObjectSource

object: INaturalLight or IStandardGeneralSky interface

GetLongitudeObjectSource Method

Description

It returns an ILongitudeObjectSource interface allowing getting or setting the longitude parameters of an ambient

source.

Syntax

object. GetLongitudeObjectSource() As ILongitudeObjectSource

object: INaturalLight or IStandardGeneralSky interface

Properties

Date Property

Description

With it, you can get or set the date of an Ambient Source.

Syntax

object. Date() As Variant

object: INaturalLight or IStandardGeneralSky interface

Turbidity Property

Description

With it, you can get or set the turbidity value of a Natural Light ambient Source.

Syntax

object. Turbity() As Double

object: INaturalLight interface

IStandardGeneralSky interface

With the IStandardGeneralSky Interface , you can manage a CIE Standard General Sky ambient source.

Page 18: OptisWorks Automation 2014 SP1 - Optis Portal

Page 18 of 61 OptisWorks Automation User Guide

Methods

GetDateTimeObjectSource Method

Description

It returns an IDateTimeObjectSource interface allowing getting or setting the date and time parameters of an

ambient source.

Syntax

object. GetDateTimeObjectSource() As IDateTimeObjectSource

object: INaturalLight or IStandardGeneralSky interface

GetLatitudeObjectSource Method

Description

It returns an ILatitudeObjectSource interface allowing getting or setting the latitude parameters of an ambient

source.

Syntax

object. GetLatitudeObjectSource() As ILatitudeObjectSource

object: INaturalLight or IStandardGeneralSky interface

GetLongitudeObjectSource Method

Description

It returns an ILongitudeObjectSource interface allowing getting or setting the longitude parameters of an ambient

source.

Syntax

object. GetLongitudeObjectSource() As ILongitudeObjectSource

object: INaturalLight or IStandardGeneralSky interface

Properties

Date Property

Description

With it, you can get or set the date of an Ambient Source.

Syntax

object. Date() As Variant

object: INaturalLight or IStandardGeneralSky interface

Luminance Property

Description

With it, you can get or set the luminance value of an ambient source.

Syntax

object. Luminance() As Double

object: IEnvironment, IStandardGeneralSky, IStandardOvercastSky or IUniform interface

IStandardOvercastSky Interface

With the IStandardOvercastSky Interface, you can manage a CIE Standard Overcast Sky ambient source.

Properties

Luminance Property

Description

With it, you can get or set the luminance value of an ambient source.

Page 19: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 19 of 61

Syntax

object. Luminance() As Double

object: IEnvironment, IStandardGeneralSky, IStandardOvercastSky or IUniform interface

IUniform Interface

With the IUniform Interface, you can manage an Uniform ambient source.

Properties

Luminance Property

Description

With it, you can get or set the luminance value of an ambient source.

Syntax

object. Luminance() As Double

object: IEnvironment, IStandardGeneralSky, IStandardOvercastSky or IUniform interface

Page 20: OptisWorks Automation 2014 SP1 - Optis Portal

Page 20 of 61 OptisWorks Automation User Guide

OptisWorksCatalog Object

OptisWorksCatalog object is part of the OptisWorks catalog library.

Methods

Find Method

Description

It returns an object OptisWorksInstance

Syntax

object.Find(pDispSearch As Object)

object: OptisWorksCatalog object

pDispSearch: SolidWorks ModelDoc2 object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Sub main()

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

End Sub

Page 21: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 21 of 61

OptisWorksDetectors Object

OptisWorksDetectors object is part of the OptisWorks library.

Methods

GetCategory Method

Description

With it, you can get the category of a detector between an illuminance, a luminance, an intensity and a polar

intensity detector.

Syntax

object. GetCategory() As Long

object: OptisWorksDetectors object

GetCategory: Set/Returned long value, type of detector category between illuminance

(owDetectorCategoryIlluminance), luminance (owDetectorCategoryLuminance), intensity

(owDetectorCategoryIntensity), polar intensity (owDetectorCategoryPolarIntensity) or rays map

(owDetectorCategoryRaysMap).

Example

Dim swApp As Object

Dim modelDoc As ModelDoc2

Dim OWcatalog As OptisWorksCatalog

Dim OWinstance As OptisWorksInstance

Dim Detector As OptisWorksDetectors

Dim IlluminanceDetector As IIlluminanceDetector

Set swApp = Application.SldWorks

Set modelDoc = swApp.ActiveDoc

Set OWcatalog = CreateObject("optisworkscatalog.catalog")

Set OWinstance = OWcatalog.Find(modelDoc)

MyDetector = "detector"

Set Detector = OWinstance.GetDetector(MyDetector)

If Detector.GetCategory = owDetectorCategoryIlluminance Then

Set IlluminanceDetector = Detector

End If

Page 22: OptisWorks Automation 2014 SP1 - Optis Portal

Page 22 of 61 OptisWorks Automation User Guide

OptisWorks Detector Interfaces

IIlluminanceDetector Interface

With the IIlluminanceDetector interface , you can manage an illuminance detector.

Properties

SamplingX Property

Description

With it, you can get or set the sampling in the X direction of the detector.

Syntax

object.SamplingX() As Double

object: OptisWorksDetectors object

SamplingX: set/returned double value

SamplingY Property

Description

With it, you can get or set the sampling in the Y direction of the detector.

Syntax

object.SamplingY() As Double

object: OptisWorksDetectors object

SamplingY: set/returned double value

IIntensityDetector Interface

With the IIntensity Interface, you can manage an intensity detector.

Properties

SamplingX Property

Description

With it, you can get or set the sampling in the X direction of the detector.

Syntax

object.SamplingX() As Double

object: OptisWorksDetectors object

SamplingX: set/returned double value

SamplingY Property

Description

With it, you can get or set the sampling in the Y direction of the detector.

Syntax

object.SamplingY() As Double

object: OptisWorksDetectors object

SamplingY: set/returned double value

ILuminanceDetector Interface

With the ILuminanceDetector Interface, you can manage a luminance detector.

Page 23: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 23 of 61

Properties

IntegrationAngle

Description

With it, you can get or set the integration angle value of a luminance or a polar intensity detector.

Syntax

object.IntegrationAngle() As Double

object: OptisWorksDetectors object

IntegrationAngle: set/returned double value, between 0 and 89 degrees.

Precision Property

Description

With it, you can get or set the precision value of a luminance detector.

Syntax

object. Precision() As Double

object: OptisWorksDetectors object

Precision: set/returned double value between 0 and 100%.

SamplingX Property

Description

With it, you can get or set the sampling in the X direction of the detector.

Syntax

object.SamplingX() As Double

object: OptisWorksDetectors object

SamplingX: set/returned double value

SamplingY Property

Description

With it, you can get or set the sampling in the Y direction of the detector.

Syntax

object.SamplingY() As Double

object: OptisWorksDetectors object

SamplingY: set/returned double value

IPolarIntensityDetector Interface

With the IPolarIntensityDetector Interface, you can manage a polar intensity detector.

Properties

IntegrationAngle

Description

With it, you can get or set the integration angle value of a luminance or a polar intensity detector.

Syntax

object.IntegrationAngle() As Double

object: OptisWorksDetectors object

IntegrationAngle: set/returned double value, between 0 and 89 degrees.

Page 24: OptisWorks Automation 2014 SP1 - Optis Portal

Page 24 of 61 OptisWorks Automation User Guide

SamplingCPlane

Description

With it, you can get or set the sampling value in the C plane direction of the detector.

Syntax

object.SamplingCPlane() As Double

object: OptisWorksDetectors object

SamplingCPlane: set/returned double value.

SamplingGPlane

Description

With it, you can get or set the sampling value in the G plane direction of the detector.

Syntax

object.SamplingGPlane() As Double

object: OptisWorksDetectors object

Precision: set/returned double value.

Example

Example Detector Interface

Dim swApp As Object

Dim modelDoc As ModelDoc2

Dim OWcatalog As OptisWorksCatalog

Dim OWinstance As OptisWorksInstance

Dim Detector As OptisWorksDetectors

Dim IlluminanceDetector As IIlluminanceDetector

Dim Simulation As OptisWorksIlluminance

Dim DirectSimulation As IIlluminanceSimulation2

Sub main()

Set swApp = Application.SldWorks

Set modelDoc = swApp.ActiveDoc

Set OWcatalog = CreateObject("optisworkscatalog.catalog")

Set OWinstance = OWcatalog.Find(modelDoc)

Set Detector = OWinstance.GetDetector("detector")

If Detector.GetCategory = owDetectorCategoryIlluminance Then

Set IlluminanceDetector = Detector

End If

IlluminanceDetector.SamplingX = 50

IlluminanceDetector.SamplingY = 50

Set Simulation = OWinstance.GetIlluminance()

Set DirectSimulation = Simulation

If DirectSimulation.Initialize Then

If DirectSimulation.AddConfig("Défaut") Then

Page 25: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 25 of 61

retval = DirectSimulation.SetNumberRays(1000000)

retval = DirectSimulation.ProcessSimulation2(0)

End If

DirectSimulation.Uninitialize

End If

End Sub

Page 26: OptisWorks Automation 2014 SP1 - Optis Portal

Page 26 of 61 OptisWorks Automation User Guide

OptisWorksDirectSimulation Object

OptisWorksDirectSimulation object is part of the OptisWorks library.

Methods

get3dmapfilename Method

Description

With it, you can get 3D Map filename.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.Get3dMapFilename(BSTR bstrFileName) As Boolean

object: OptisWorksDirectSimulation object

bstrFileName : 3D map filename string (output)

getdetectorresultfilename Method

Description

With it, you can get the result filename of a detector.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.GetDetectorResultFilename(BSTR bstrDetectorName As String, BSTR bstrFileName As String) As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

bstrDetectorName: Detector name string (input)

bstrFileName: Path of the XMP filename for the last result (output)

getrayfilename Method

Description

With it, you can get the ray filename.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.GetRayFilename(BSTR bstrFileName) As Boolean

object: OptisWorksDirectSimulation object

bstrFileName: Ray filename string (output)

getreportfilename Method

Description

With it, you can get the report filename.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.GetReportFilename(BSTR bstrFileName) As Boolean

object: OptisWorksDirectSimulation object

bstrFileName: Report filename string (output)

initialize Method

Description

It initializes a direct or an inverse simulation.

It returns a boolean value : true if it succeeded, false otherwise.

Page 27: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 27 of 61

Syntax

object.Initialize() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

IsSuppress Method

Description

It checks whether a simulation is suppressed or not.

It returns True if the simulation is suppressed and false otherwise.

Syntax

object.IsSuppress() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

Run Method

Description

It processes a direct or an inverse simulation.

Syntax

object.Run() As Long

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

Set3DMap Method

Description

It sets the generation of a 3D map for the direct simulation.

Syntax

object.Set3DMap(bChecked As Long) As Boolean

object: OptisWorksDirectSimulation object

bChecked: 1 to generate a 3D map during the direct simulation, 0 otherwise.

SetNumberRays Method

Description

It sets the number of rays used in the direct simulation.

Syntax

object.SetNumberRays(dNumberRays As Double) As Boolean

object: OptisWorksDirectSimulation object

dNumberRays: number of rays

SetRayFile Method

Description

It sets the generation of a ray file for the direct simulation.

Syntax

object.SetRayFile(bChecked As Long) As Boolean

object: OptisWorksDirectSimulation object

bChecked: 1 to generate a ray file during the direct simulation, 0 otherwise.

Page 28: OptisWorks Automation 2014 SP1 - Optis Portal

Page 28 of 61 OptisWorks Automation User Guide

uninitialize Method

Description

It uninitializes a direct or an inverse simulation.

It returns a boolean value : true if it succeeded, false otherwise

Syntax

object.Uninitialize() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

Example

Example OptisWorksDirectSimulation

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim ModelDoc As ModelDoc2

'---- Definitions for OptisWorks

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim DirectSimulation As OptisWorksDirectSimulation

Sub main()

On Error GoTo ErrorHandler

Dim retval

Dim strMap As String

Dim str3DMapFilename As String

Dim strRayFilename As String

Dim strReportFilename As String

Dim strDetectorName As String

strDetectorName = "Sensor"

Dim MyConfig(3)

MyConfig(0) = "Default"

MyConfig(1) = "MyConfig1"

MyConfig(2) = "MyConfig2"

MyConfig(3) = "MyConfig3"

Dim RayNumber(3)

RayNumber(0) = 1000

RayNumber(1) = 10000

RayNumber(2) = 100000

RayNumber(3) = 1000000

Page 29: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 29 of 61

Dim treeDMap(3)

treeDMap(0) = 0

treeDMap(1) = 1

treeDMap(2) = 0

treeDMap(3) = 1

Dim rayfile(3)

rayfile(0) = 1

rayfile(1) = 0

rayfile(2) = 1

rayfile(3) = 0

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(Part)

Set DirectSimulation = Instance.GetDirectSimulation

retval = DirectSimulation.Initialize()

For i = 0 To 3

boolstatus = Part.Extension.SelectByID2(MyConfig(i), "CONFIGURATIONS", 0, 0, 0, False,

0, Nothing, 0)

Part.ShowConfiguration MyConfig(i)

boolstatus = Part.EditRebuild3

retval = DirectSimulation.SetNumberRays(RayNumber(i))

retval = DirectSimulation.Set3DMap(treeDMap(i))

retval = DirectSimulation.SetRayFile(rayfile(i))

If Not DirectSimulation.IsSuppress() Then

retval = DirectSimulation.Run()

End If

retval = DirectSimulation.GetDetectorResultFilename(strDetectorName, strMap)

retval = DirectSimulation.Get3DMapFilename(str3DMapFilename)

retval = DirectSimulation.GetRayFilename(strRayFilename)

retval = DirectSimulation.GetReportFilename(strReportFilename)

Next

retval = DirectSimulation.Uninitialize()

ErrorHandler:

If Err.Number <> 0 Then

Msg = "Error number : " & Str(Err.Number) & " has been generated by " _

& Err.Source & Chr(13) & "Description : " Err.Description

MsgBox Msg, , "Erreur", Err.HelpFile, Err.HelpContext

Page 30: OptisWorks Automation 2014 SP1 - Optis Portal

Page 30 of 61 OptisWorks Automation User Guide

End If

Set DirectSimulation = Nothing

Set Catalog = Nothing

Set Instance = Nothing

End Sub

Page 31: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 31 of 61

OptisWorksFaces Object

OptisWorksFaces object is part of the OptisWorks library.

Methods

AddFace Method

Description

It adds a selected face to a list of faces

Syntax

object.AddFace(pDispatch As Object) As Boolean

object: OptisWorksFaces object

pDispatch: object referring to a face of a part geometry in SolidWorks (input).

Example

Set swApp = CreateObject("SldWorks.Application")

Set Part = swApp.ActiveDoc

Set OWcatalog = CreateObject("optisworkscatalog.catalog")

Set OWinstance = OWcatalog.Find(Part)

Set faces = OWinstance.GetOptisWorksFaces

faceName = "My Face"

Set MySWFace = Part.GetEntityByName(faceName, 2) 'Get Face by its name

retval = faces.AddFace(MySWFace)

ClearFaces Method

Description

It clears the list of selected faces.

It returns true if succeeded, false otherwise.

Syntax

object.ClearFaces() As Boolean

object: OptisWorksFaces object

ClearOpticalProperties Method

Description

It clears the set optical properties.

It returns true if succeeded, false otherwise.

Syntax

object.ClearOpticalProperties() As Boolean

object: OptisWorksFaces object

SetExternalMaterial Method

Description

It sets the external material to be applied to a selected group of faces.

Syntax

object. SetExternalMaterial (bstrFullPathFileName As String, ActionType As Long) As Boolean

object: OptisWorksFaces object

bstrFullPathFileName: full path to the external material filename (input)

Page 32: OptisWorks Automation 2014 SP1 - Optis Portal

Page 32 of 61 OptisWorks Automation User Guide

ActionType: type of action between add the optical property (owOpticalPropertyAdd), do nothing

(owOpticalPropertyNone) and remove the optical property (owOpticalPropertyRemove).

SetInternalMaterial Method

Description

It sets the internal material to be applied to a selected group of faces.

Syntax

object. SetInternalMaterial (bstrFullPathFileName As String, ActionType As Long) As Boolean

object: OptisWorksFaces object

bstrFullPathFileName: full path to the internal material filename (input)

ActionType: type of action between add the optical property (owOpticalPropertyAdd), do nothing

(owOpticalPropertyNone) and remove the optical property (owOpticalPropertyRemove).

SetOpticalPropertiesToFaces Method

Description

It applies the set optical properties to a selected group of faces.

It returns true if succeeded, false otherwise.

Syntax

object. SetOpticalPropertiesToFaces() As Boolean

object: OptisWorksFaces object

SetSurfaceQuality Method

Description

It sets the surface quality to be applied to a selected group of faces.

Syntax

object. SetSurfaceQuality (bstrFullPathFileName As String, ActionType As Long) As Boolean

object: OptisWorksFaces object

bstrFullPathFileName: full path to the surface quality filename (input)

ActionType: type of action between add the optical property (owOpticalPropertyAdd), do nothing

(owOpticalPropertyNone) and remove the optical property (owOpticalPropertyRemove).

Example

Example OptisWorksFaces

Dim swApp As Object

Dim modelDoc As ModelDoc2

Dim MySWFace As Object

Dim OWcatalog As OptisWorksCatalog

Dim OWinstance As OptisWorksInstance

Dim faces As OptisWorksFaces

Sub main()

Set swApp = Application.SldWorks

Set modelDoc = swApp.ActiveDoc

Set OWcatalog = CreateObject("optisworkscatalog.catalog")

Set OWinstance = OWcatalog.Find(modelDoc)

Set faces = OWinstance.GetOptisWorksFaces

Page 33: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 33 of 61

retval = faces.SetExternalMaterial("C:\Program Files

(x86)\OPTIS\Library\Material\AIR.material", owOpticalPropertyAdd)

retval = faces.SetInternalMaterial("C:\Program Files

(x86)\OPTIS\Library\Material\POLYCARBONATE.material", owOpticalPropertyAdd)

retval = faces.SetSurfaceQuality("C:\Program Files

(x86)\OPTIS\Library\Surface\Optical_Polished.opt", owOpticalPropertyAdd)

For i = 1 To 6

faceName = "Face" & i

Set MySWFace = modelDoc.GetEntityByName(faceName, 2) 'Get Face by its name

retval = faces.AddFace(MySWFace)

Next i

retval = faces.SetOpticalPropertiesToFaces

retval = faces.ClearFaces

retval = faces.ClearOpticalProperties

End Sub

OptisWorksInstance Object

OptisWorksInstance object is part of the OptisWorks library.

Page 34: OptisWorks Automation 2014 SP1 - Optis Portal

Page 34 of 61 OptisWorks Automation User Guide

Methods

Get3DTextures

Description

It returns an OptisWorks3DTextures object.

Syntax

object.Get3DTextures()

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim 3DTexture As OptisWorks3DTextures

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("optisworkscatalog.catalog")

Set Instance = Catalog.Find(ModelDoc)

Set 3DTexture = Instance.Get3DTextures

GetAmbientSources Method

Description

It returns an OptisWorksAmbientSources object.

Syntax

object.GetAmbientSources

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim AmbientSources As OptisWorksAmbientSources

Sub main()

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set AmbientSources = Instance.GetAmbientSources

End Sub

GetDetector Method

Description

It returns an OptisWorksDetectors object.

Syntax

object.GetDetector()

Page 35: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 35 of 61

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim Detector As OptisWorksDetectors

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("optisworkscatalog.catalog")

Set Instance = Catalog.Find(ModelDoc)

DetectorName = ”MyDetector”

Set Detector = Instance.GetDetector(DetectorName)

GetDirectSimulation Method

Description

It returns an OptisWorksDirectSimulation object.

Syntax

object.GetDirectSimulation()

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim DirectSimulation As OptisWorksDirectSimulation

Sub main()

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set DirectSimulation = Instance.GetDirectSimulation()

End Sub

GetInverseSimulation Method

Description

It returns an OptisWorksInverseSimulation.

Syntax

object.GetInverseSimulation()

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Page 36: OptisWorks Automation 2014 SP1 - Optis Portal

Page 36 of 61 OptisWorks Automation User Guide

Dim InverseSimulation As OptisWorksInverseSimulation

Sub main()

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set InverseSimulation = Instance.GetInverseSimulation()

End Sub

GetOptisWorksFaces Method

Description

It returns an OptisWorksFaces object.

Syntax

object.GetOptisWorksFaces()

object: OptisWorksInstance object

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim faces As OptisWorksFaces

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("optisworkscatalog.catalog")

Set Instance = Catalog.Find(ModelDoc)

Set faces = Instance.GetOptisWorksFaces

GetPartPreferences

Description

It returns an OptisWorksPartPreferences object.

Syntax

object.GetPartPreferences()

object: OptisWorksInstance object

Remark

GetPartPreferences object can only be used in a script for a part, not for an assembly.

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim PartPreferences As OptisWorksPartPreferences

Sub main()

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Page 37: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 37 of 61

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set PartPreferences = Instance.GetPartPreferences()

End Sub

GetSurfaceSource Method

Description

It returns an OptisWorksSurfaceSource object.

Syntax

object.GetSurfaceSource(BSTR Name As String)

object: OptisWorksInstance object

Name: Name of the surface source

Remark

GetSurfaceSource object can only be used in a script for a part, not for an assembly.

Example

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim SurfaceSource As OptisWorksSurfaceSource

Sub main()

Dim strName As String

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

strName = "source1"

Set SurfaceSource = Instance.GetSurfaceSource(strName)

End Sub

Page 38: OptisWorks Automation 2014 SP1 - Optis Portal

Page 38 of 61 OptisWorks Automation User Guide

OptisWorksInverseSimulation Object

OptisWorksInverseSimulation object is part of the OptisWorks library.

Methods

getdetectorresultfilename Method

Description

With it, you can get the result filename of a detector.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.GetDetectorResultFilename(BSTR bstrDetectorName As String, BSTR bstrFileName As String) As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

bstrDetectorName: Detector name string (input)

bstrFileName: Path of the XMP filename for the last result (output)

initialize Method

Description

It initializes a direct or an inverse simulation.

It returns a boolean value : true if it succeeded, false otherwise.

Syntax

object.Initialize() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

IsSuppress Method

Description

It checks whether a simulation is suppressed or not.

It returns True if the simulation is suppressed and false otherwise.

Syntax

object.IsSuppress() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

Run Method

Description

It processes a direct or an inverse simulation.

Syntax

object.Run() As Long

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

uninitialize Method

Description

It uninitializes a direct or an inverse simulation.

It returns a boolean value : true if it succeeded, false otherwise

Syntax

object.Uninitialize() As Boolean

object: OptisWorksDirectSimulation or OptisWorksInverseSimulation object

Page 39: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 39 of 61

Example

Example OptisWorksInverseSimulation

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim ModelDoc As ModelDoc2

'---- Definitions for OptisWorks

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim InverseSimulation As OptisWorksInverseSimulation

Sub main()

On Error GoTo ErrorHandler

Dim retval

Dim strMap As String

Dim strDetectorName As String

strDetectorName = "Sensor"

Dim MyConfig(3)

MyConfig(0) = "Default"

MyConfig(1) = "MyConfig1"

MyConfig(2) = "MyConfig2"

MyConfig(3) = "MyConfig3"

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(Part)

Set InverseSimulation = Instance.GetInverseSimulation

retval = InverseSimulation.Initialize()

For i = 0 To 3

boolstatus = Part.Extension.SelectByID2(MyConfig(i), "CONFIGURATIONS", 0, 0, 0, False,

0, Nothing, 0)

Part.ShowConfiguration MyConfig(i)

boolstatus = Part.EditRebuild3

If Not InverseSimulation.IsSuppress() Then

Page 40: OptisWorks Automation 2014 SP1 - Optis Portal

Page 40 of 61 OptisWorks Automation User Guide

retval = InverseSimulation.Run()

End If

retval = InverseSimulation.GetDetectorResultFilename(strDetectorName, strMap)

Next

retval = InverseSimulation.Uninitialize()

ErrorHandler:

If Err.Number <> 0 Then

Msg = "Error number : " & Str(Err.Number) & " has been generated by " _

& Err.Source & Chr(13) & "Description : " Err.Description

MsgBox Msg, , "Erreur", Err.HelpFile, Err.HelpContext

End If

Set InverseSimulation = Nothing

Set Catalog = Nothing

Set Instance = Nothing

End Sub

Page 41: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 41 of 61

OptisWorksPartPreferences Object

OptisWorksPartPreferences object is part of the OptisWorks library.

Methods

ExternalMaterial Property

Description

With it, you can set or get the external material file name.

Syntax

object.ExternalMaterial As String

object: OptisWorksPartPreferences object

ExternalMaterial: Set/Returned material filename string.

InternalMaterial Property

Description

With it, you can set or get the internal material file name.

Syntax

object.InternalMaterial As String

object: OptisWorksPartPreferences object

InternalMaterial: Set/Returned material filename string.

SurfaceQuality Property

Description

With it, you can set or get the surface quality file name.

Syntax

object.SurfaceQuality As String

object: OptisWorksPartPreferences object

SurfaceQuality: Set/Returned surface quality filename string.

Example

Example OptisWorksPartPreferences

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim PartPreferences As OptisWorksPartPreferences

Sub main()

Dim strName As String

Dim strMsg As String

On Error GoTo ErrorHandler

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Page 42: OptisWorks Automation 2014 SP1 - Optis Portal

Page 42 of 61 OptisWorks Automation User Guide

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set PartPreferences = Instance.GetPartPreferences()

MsgBox (PartPreferences.InternalMaterial)

PartPreferences.InternalMaterial = "C:\Program Files

(x86)\OPTIS\Library\Material\POLYCARBONATE.material"

MsgBox (PartPreferences.InternalMaterial)

MsgBox (PartPreferences.ExternalMaterial)

PartPreferences.ExternalMaterial = "C:\Program Files

(x86)\OPTIS\Library\Material\Water.material"

MsgBox (PartPreferences.ExternalMaterial)

MsgBox (PartPreferences.SurfaceQuality)

PartPreferences.SurfaceQuality = "C:\Program Files

(x86)\OPTIS\Library\Surface\Mirror\Example.mirror"

MsgBox (PartPreferences.SurfaceQuality)

ErrorHandler:

If Err.Number <> 0 Then

strMsg = "Error number : " & Str(Err.Number) & " has been generated by " _

& Err.Source & Chr(13) & "Description : " & Err.Description

MsgBox strMsg, , "Erreur", Err.HelpFile, Err.HelpContext

End If

Set PartPreferences = Nothing

Set Catalog = Nothing

Set Instance = Nothing

Set ModelDoc = Nothing

End Sub

Page 43: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 43 of 61

OptisWorksSurfaceSource Object

OptisWorksSurfaceSource object is part of the OptisWorks library.

Properties

FileName Property

Description

With it, you can set or get the filename of the library for the spectrum of a source.

Syntax

object.FileName As String

object: OptisWorksSurfaceSource object

Filename: Set/Returned string value

Flux Property

Description

With it, you can set or get the flux value of a surface source.

Syntax

object.Flux As Double

object: OptisWorksSurfaceSource object

Flux: Set/Returned double value

FluxType Property

Description

With it, you can set or get the type of flux for a surface source (radiant or luminous).

Syntax

object.FluxType As Long

object: OptisWorksSurfaceSource object

FluxType: Set/Returned long value, type of flux between radiant (owRadiant) or luminous (owLuminous)

SpectrumType

Description

It sets the type of spectrum of the existing surface source to modify the related parameters.

The argument value is a string containing the name of the spectrum type.

Argument Value

Monochromatic: for single wavelength surface source.

Blackbody: for blackbody spectrum surface source.

Library: for library spectrum surface source.

Example

Dim inValSpeType As IpfcArgValue

Dim MyCMpfcArgument As New CMpfcArgument

Set inValSpeType = MyCMpfcArgument.CreateStringArgValue("Monochromatic")

Dim InpSpeType As IpfcArgument

Dim MyCCpfcArgument As New CCpfcArgument

Set InpSpeType = MyCCpfcArgument.Create("SpectrumType", inValSpeType)

Call InputArguments.Append(InpSpeType)

Page 44: OptisWorks Automation 2014 SP1 - Optis Portal

Page 44 of 61 OptisWorks Automation User Guide

Temperature Property

Description

With it, you can set or get the temperature of a blackbody type surface source.

Syntax

object.Temperature As Double

object: OptisWorksSurfaceSource object

Temperature : Set/Returned double value

Wavelength Property

Description

With it, you can set or get the wavelength of a monochromatic type surface source.

Syntax

object.Wavelength As Double

object: OptisWorksSurfaceSource object

Wavelength : Set/Returned double value

Example

Example OptisWorksSurfaceSource

Dim swApp As Object

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim SurfaceSource As OptisWorksSurfaceSource

Sub main()

Dim strName As String

Dim strMsg As String

strName = "Ambient"

On Error GoTo ErrorHandler

Set swApp = Application.SldWorks

Set ModelDoc = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set Instance = Catalog.Find(ModelDoc)

Set SurfaceSource = Instance.GetSurfaceSource(strName)

If IsNull(SurfaceSource) Then GoTo ErrorHandler

If SurfaceSource.FluxType = owRadiant Then

MsgBox SurfaceSource.Flux, vbOKOnly, "Value source Radian"

ElseIf SurfaceSource.FluxType = owLuminous Then

MsgBox SurfaceSource.Flux, vbOKOnly, "Value source Luminous"

Else

Page 45: OptisWorks Automation 2014 SP1 - Optis Portal

APIs Page 45 of 61

MsgBox SurfaceSource.Flux, vbOKOnly, "Value source none"

End If

SurfaceSource.Flux = 2000

MsgBox SurfaceSource.Flux, vbOKOnly, "New value for the source"

If SurfaceSource.SpectrumType = owMonochromatic Then

MsgBox SurfaceSource.Wavelength, vbOKOnly, "Value source spectrum Monochromatic"

SurfaceSource.Wavelength = 800

MsgBox SurfaceSource.Wavelength, vbOKOnly, "New value for the spectrum Monochromatic"

ElseIf SurfaceSource.SpectrumType = owBlackbody Then

MsgBox SurfaceSource.Temperature, vbOKOnly, "Value source spectrum Blackbody"

SurfaceSource.Temperature = 2800

MsgBox SurfaceSource.Temperature, vbOKOnly, "New value for the spectrum Blackbody"

ElseIf SurfaceSource.SpectrumType = owLibrary Then

MsgBox SurfaceSource.FileName, vbOKOnly, "Value source spectrum Library"

SurfaceSource.FileName = "C:\Program Files

(x86)\OPTIS\Library\Spectrum\yellow.spectrum"

MsgBox SurfaceSource.FileName, vbOKOnly, "New value for the spectrum Library"

Else

MsgBox 0, vbOKOnly, "Value source spectrum none"

End If

ErrorHandler:

If Err.Number <> 0 Then

strMsg = "Error number : " & Str(Err.Number) & " has been generated by " _

& Err.Source & Chr(13) & "Description : " & Err.Description

MsgBox strMsg, , "Erreur", Err.HelpFile, Err.HelpContext

End If

Set SurfaceSource = Nothing

Set Catalog = Nothing

Set Instance = Nothing

Set ModelDoc = Nothing

End Sub

Page 46: OptisWorks Automation 2014 SP1 - Optis Portal

Page 46 of 61 OptisWorks Automation User Guide

TUTORIAL

You must have the LM2, OPTIM1 and COL.

The aim of the following tutorial is to give the user a first experience of what the OPTIM1 option can achieve.

You are about to learn how to access to an existing OptisWorks 2007 simulation feature and how to update it from a script, as well as controlling the OPTIS XMP Viewer application using a script. Finally, this tutorial show to interact between OptisWorks 2007 and the OPTIS XMP Viewer using the same script.

1h30

You are going to generate a first macro from scratches creating the image of a blackbody at different temperature between 1000K and 12500K.

Then with a second script, you are going to optimize the temperature of a blackbody to reduce the difference between its color and a given color.

First Macro Lesson 1: Recording Events

1. Open the Automation file.

This file is located in OW_Tutorials_Automation.zip

(http://portal.optis-world.com/documentation/UG/OW/ZIP/OW_Tutorials_Automation.zip).

The file consists of a source part named source1 that represents a blackbody with the temperature of 1000°K

and a Luminance detector named LumMeter.

Because the temperature of the blackbody can only be changed within the part containing the source, we need

to record the procedure to select the part, open it and then rebuilt the assembly.

This next step will give us the SolidWorks script that we are going to modify by writing manually the OptisWorks

APIs to change the source temperature and proceed an inverse simulation to get luminance data.

2. Click Tools, Macro, Record or click Record/Pause Macro .

3. In the FeatureManager design tree, right-click the SurfaceSource part, and then select Open Part .

4. In the FeatureManager design tree, select the part name.

5. To reactivate the assembly, click Window, Automation.SLDASM.

6. Click Tools, Macro, Stop or click Stop Macro to stop the recording.

7. Save your macro, for example by naming it AutomationMacro1.swp.

8. To edit your macro, click Tools, Macro, Edit or click Edit Macro .

9. Select your macro, and then click Open.

You can visualize the script of the events you have just recorded in Microsoft Visual Basic.

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Dim Feature As Object

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Page 47: OptisWorks Automation 2014 SP1 - Optis Portal

Tutorial Page 47 of 61

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectBy ID2 ("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

boolstatus = Part.Extension.SelectBy ID2 ("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

Part.OpenCompFile

Set Part = swApp.OpenDoc6("C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings)

swApp.OpenDoc6 "C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings

swApp.ActiveDoc.ActiveView.FrameLeft=0

swApp.ActiveDoc.ActiveView.FrameTop = 0

swApp.ActiveDoc.ActiveView.FrameState = 1

swApp.ActiveDoc.ActiveView.FrameState = 1

Set Part = swApp.ActivateDoc2("SurfaceSource.SLDPRT", False, longstatus)

Part.ClearSelection2 True

boolstatus = Part.Extension.SelectByID2("SurfaceSource.slrdt", "COMPONENT", 0, 0, 0,

False, 0, Nothing, 0)

swApp.ActiveDoc.ActiveView.FrameLeft = 0

swApp.ActiveDoc.ActiveView.FrameTop = 0

swApp.ActiveDoc.ActiveView.FrameState = 1

swApp.ActiveDoc.ActiveView.FrameState = 1

Set Part = swApp.ActivateDoc2("Automation.SLDASM", False, longstatus)

End Sub

You can see the comments on the script to decrypt it and delete the lines that you do not want.

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

Dim Feature As Object

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set SelMgr = Part.SelectionManager

'---- Opening the part containing the source "source 1"

boolstatus = Part.Extension.SelectByID2("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

Part.OpenCompFile

Set Part =

swApp.OpenDoc6("C:\ProgramFiles\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSourc

e.SLDPRT", 1, 0, "", longstatus, longwarnings)

swApp.OpenDoc6 "C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings

Set Part = swApp.ActivateDoc2("GS_SurfaceSource.SLDPRT", False, longstatus)

Page 48: OptisWorks Automation 2014 SP1 - Optis Portal

Page 48 of 61 OptisWorks Automation User Guide

'---- Back to the assembly. Note that the rebuild is automatic

Set Part = swApp.ActivateDoc2("Automation.SLDASM, False, longstatus)

End Sub

First Macro Lesson 2: Modifying an Existing Macro

This step shows how to update the macro to create a loop, change the temperature of the blackbody source, run an inverse simulation to get luminance data and ave luminance data as an image.

To change the temperature of the source, do the following:

1. To check the references in Microsoft Visual Basic to execute your script, in this program, click Tools,

References.

A box appears.

2. Select the OptisWorks Catalog 1.0 Type Library check box and the OptisWorks script 1.0 Type Library check box.

3. If these references are not in the list, do the following:

1. Click Browse.

2. In the OptisWorks 20XX folder, itself in Optis, select OptisWorksCatalog.dll.

3. If you have a 64 bits operating system version, select OptisWorks.tlb.

Before modifying the script, we need to declare some variables to use the OptisWorks APIs.

4. Do as follows.

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

'---- Definitions for OptisWorks

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim Surface Source As OptisWorksSurfaceSource

Dim InverseSimulation As OptisWorksInverseSimulation

Dim OPTISViewer As Object

Sub main()

Dim temperature, tempMin, tempMax, tempStep

Dim strSourceName

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set Catalog = CreateObject ("OptisWorksCatalog.Catalog")

strSourceName = "source1"

tempMin = 1000

Page 49: OptisWorks Automation 2014 SP1 - Optis Portal

Tutorial Page 49 of 61

tempMax = 12500

tempStep = 500

'---- For modification of the source temperature, with the part containing the source

"source1"

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

Part.OpenCompFile

Set Part = swApp.OpenDoc6("C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings)

Set Part = swApp.ActivateDoc2 ("SurfaceSource", False, longstatus)

Set ModelDoc = swApp.ActiveDoc

Set Instance = Ctalog.Find (ModelDoc)

Set SurfaceSourc = Instance.GetSurfaceSource (strSourceName)

'---- For inverse simulation with the assembly

Set Part = swApp.ActivateDoc2 ("Automation.SLDASM", False, longstatus)

Set ModelDoc = swApp.ActivateDoc

Set Instance = Catalog.Find (ModelDoc)

Set InverseSimulation = Instance.GetInverseSimulation ()

End Sub

For OptisWorks Studio, the definition of swApp is different: Set swApp =

GetObject(Class:="OptisWorksStudio.Application")

As the temperature will change between 1000 and 12500°K, we use a For loop that will increment the

temperature by 500°K steps.

Then, we use OptisWorks APIs to modify the temperature of the source, placing the script just before the

assembly reactivation part.

For temperature = tempMin To tempMax Step tempStep

SurfaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

Next temperature

End Sub

To run an inverse simulation, and get the luminance data for each temperature of the loop steps, do as follows.

The following code uses OptisWorks APIs to run inverse simulation and get the luminance data for each

temperature of the loop steps.

retval = InverseSimulation.Initialize ()

For temperature = tempMin To tempMax Step tempStep

SurfaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

Page 50: OptisWorks Automation 2014 SP1 - Optis Portal

Page 50 of 61 OptisWorks Automation User Guide

If Not InverseSimulation.IsSuppress Then

retval = InverseSimulation.Run ()

retval = InverseSimulation.GetDetectorResultFilename (strDetectorName, strMap)

strFileOpen = strMap

End if

Next temperature

retval = InverseSimulation.Unitialize ()

To save the luminance data as an image, do as follows.

To finish the script, we want to save the data from the inverse simulation as a jpeg image.

To do so, we need to use some APIs from OptisLabs' Virtual Photometric Lab.

retval = InverseSimulation.Initialize ()

For temperature = tempMin To tempMax Step tempStep

SurfaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

If Not InverseSimulation.IsSuppress Then

retval= InverseSimulation.Run()

retval = InverseSimulation.GetDetectorResultFilename(strDetectorName,strMap)

strFileOpen = strMap

OPTISViewer.OpenFile strFileOpen

strFileSave = "C:\Program Files\OPTIS\Help\OptisWorks_Tutorials\Automation\Images\"&_

Format(temperature,"00000")& ".jpg"

retval=OPTISViewer.ExportXMPImage(strFileSave,3) '3 for JPEG format

End If

Next temperature

retval = InverseSimulation.Unitialize()

Make sure the path to save the images is correct.

First Macro Lesson 3: Running the Macro

1. Click Tools, Macro, Run or click Run Macro to execute the macro.

A box appears.

2. Select the macro that you want to run.

3. Click Open.

4. Open the Images folder, and then double-click View Thumbnails.

The images with the colors of each blackbody from 1000K to 12500K appear.

You can view the entire script (see page 56)

Second Macro Lesson 4: Creating a Second Macro from Scratches

Page 51: OptisWorks Automation 2014 SP1 - Optis Portal

Tutorial Page 51 of 61

1. Click Tools, Macro, New... or click New Macro .

2. Save this new blank macro as AutomationMacro2.

Microsoft Visual Basic opens.

You can visualize the blank script.

3. To check the references in Microsoft Visual Basic to execute your script, in this program, click Tools,

References.

A box appears.

4. Select OptisWorks Catalog 1.0 Type Library and OptisWorks script 1.0 Type Library.

5. If these references are not in the list, do the following:

1. Click Browse.

2. In the OptisWorks 20XX folder, itself in Optis, select OptisWorksCatalog.dll.

3. If you have a 64 bits operating system version, select OptisWorks.tlb.

Second Macro Lesson 5: Updating the Macro

With this lesson, you are about to learn how to create a measurement procedure, how to create a minimization

procedure, how to write an optimization loop and how to export the results in a file.

1. Declare the following global variables to use them in the procedures.

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

'---- Definitions for OptisWorks

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim Surface Source As OptisWorksSurfaceSource

Dim InverseSimulation As OptisWorksInverseSimulation

Dim OPTISViewer As Object

Dim strSourceName As String

Dim strDetectorName As String

2. Declare the following global variables.

These global variables are useful for the optimization algorithm

'---- Definitions for Optimizations

Dim XTarget As Double, YTarget As Double

Dim TemperatureInf As Double

Dim TemperatureMid As Double

Dim TemperatureSup As Double

Dim DeltaEInf As Double, DeltaEMid As Double

Dim DeltaESup As Double

Dim SimuInf As Boolean, SimuMid As Boolean

Dim SimuSup As Boolean

Page 52: OptisWorks Automation 2014 SP1 - Optis Portal

Page 52 of 61 OptisWorks Automation User Guide

3. To update the source and the simulation if necessary, the measurement of the color and the calculation of

DeltaE, the color difference between the measured color and the target color, do as follows.

Sub CalcDeltaE(temperature As Double, DeltaE As Double, Simu As Boolean, strSourceName As

String)

Dim retval

Dim strMap As String

Dim strFileOpen As String

Dim Measure

If Simu = False Then

SufaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

If Not InverseSimulation.IsSuppress Then

retval = InverseSimulation.Run

retval = InverseSimulation.GetDetectorResultFilename(strDetectorNames,strMap)

strFileOpen = strMap

End If

'--- Calculation of DeltaE

OPTISViewer.OpenFile strFileOpen

Measure = OPTISViewer.GetColor(0#, 0#, 1)

Delta E =

Sqr((Measure(1)-XTarget)*(Measure(1)-XTarget)+(Measure(2)-YTarget)*(Measure(2)-YTarget)

)

Simu = True

End If

End Sub

Temperature is used as an input.

It defines the temperature of the blackbody.

DeltaE is used as an output parameter.

It is computed by the procedure.

Simu is used as input and output and indicates if the result is already known by the optimization algorithm

avoiding by this way to redo a useless simulation.

4. Do as follows.

Sub Minimize(XInf As Double, YInf As Double, UpToDateInf As Boolean, _

XMid As Double, YMid As Double, UpToDateMid As Boolean, _

XMid As Double, YMid As Double, UpToDateMid As Boolean, _

XSup As Double, YSup As Double, UpToDateSup As Boolean)

If (YMid < YInf) Then ' case \

If (YMid < YSup) Then ' case \/

If (YInf < YSup) Then

XSup = XMid: YSup = YMid

XMid = (XInf + XSup) / 2: UpToDateMid = False

Else

XInf = XMid: YInf = YMid

Page 53: OptisWorks Automation 2014 SP1 - Optis Portal

Tutorial Page 53 of 61

XMid = (XInf + XSup) / 2: UpToDateMid = False

End If

Else ' case \\

XInf = XMid: YInf = YMid

XMid = XSup: YMid = YSup

XSup = XMid + (XMid - XInf): UpToDateSup = False

End If

Else ' case /

If (YMid < YSup) Then ' case //

XSup = XMid: YSup = YMid

XMid = XInf: YMid = YInf

XInf = XMid - (XSup - XMid): UpToDateInf = False

Else ' case /\

If (YInf < YSup) Then

XSup = XMid: YSup = YMid

XMid = XInf: YMid = YInf

XInf = XMid - (XSup - XMid): UpToDateInf = False

Else

XInf = XMid: YInf = YMid

XMid = XSup: YMid = YSup

XSup = XMid + (XMid - XInf): UpToDateSup = False

End If

End If

End If

Here is the optimization routine using 3 points, Inf, Mid, Sup and their X and Y coordinates.

The algorithm reduces then the range between Inf and Sup around the minimum.

The Boolean parameters are used to avoid useless evaluation of Y when X is changed.

5. To initialize the optimization parameters, do as follows.

XTarget = 0.331185

YTarget = 0.340409

TemperatureInf = 1000

TemperatureSup = 12500

TemperatureMid = (TemperatureInf + TemperatureSup)/2

SimuInf = False

SimuMid = False

SimuSup = False

XTarget and YTarget are the coordinates of the target color.

These values correspond to a 5555 degrees Kelvin blackbody.

TemperatureInf, TemperatureSup indicate the range of search.

SimuInf, SimuMid and SimuSup indicate that no simulation has been updated at this step.

6. To create the optimization loop, do as follows.

Do

Call CalcDeltaE(TemperatureInf, DeltaEInf, SimuInf, strSourceName)

Call CalcDeltaE(TemperatureMid, DeltaEMid, SimuMid, strSourceName)

f.WriteLine TemperatureInf & " " & TemperatureSup & " " & TemperatureMid & " " & DeltaEMid

Page 54: OptisWorks Automation 2014 SP1 - Optis Portal

Page 54 of 61 OptisWorks Automation User Guide

Call Minimize(TemperatureInf, DeltaEInf, SimuInf, TemperatureMid, DeltaEMid, SimuMid, _

TemperatureSup, DeltaESup, SimuSup)

Loop Until (Abs(TemperatureSup - TemperatureInf) <1)

The stopping criterion is defined by the size of the current range.

Now, we are going to export the results.

7. To create a text file that presents the optimzation steps and final results, place the following code in the

initialization part of the main procedure.

Dim fs, f

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile("C:\Program Files (x86)\OPTIS\Help\Getting Started

OptisWorks\GS_Automation\Export.txt",_ True)

8. To write current parameter values, place the following code in the loop.

f.Writeline TemperatureInf & " " & TemperatureSup & " " & TemperatureMid & " " & DeltaEMid

9. To compute the result and close the file, place the following code after the optimization.

Call CalcDeltaE(TemperatureMid,DeltaEMid,SimuMid,strSourceName)

f.WriteLine TemperatureInf & " " & TemperatureSup & " " & TemperatureMid & " " & DeltaEMid

f.Close

Second Macro Lesson 6: Presentation of the Results

This step shows how to run the macro and how to get the result.

1. Click Tools, Macro, Run or click Run Macro to execute the macro.

2. Open Microsoft Excel and the Export.txt file.

Page 55: OptisWorks Automation 2014 SP1 - Optis Portal

Tutorial Page 55 of 61

You can observe twenty optimization steps, the temperature of the blackbody corresponding to the color

coordinates set up is approximately 5554.6°K.

You can now observe the three first columns using a Stock chart that shows how the temperature range is

reduced to get the final value.

You can observe the variation of the color difference DeltaE approaching 0 by plotting the 4th column with a line

chart.

To see the entire script: second script (see page 58)

Page 56: OptisWorks Automation 2014 SP1 - Optis Portal

Page 56 of 61 OptisWorks Automation User Guide

FIRST MACRO SCRIPT

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

'---- Definitions for OptisWorks

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim SurfaceSource As OptisWorksSurfaceSource

Dim InverseSimulation As OptisWorksInverseSimulation

Dim OPTISViewer As Object

Sub main()

Dim temperature, tempMin, tempMax, tempStep

Dim strSourceName

Dim retval

Dim strMap As String

Dim strDetectorName As String

Dim strFileOpen As String

Dim strFileSave As String

Set swApp = Application.SldWorks

' Set swApp = GetObject(Class:="OptisWorksStudio.Application") for OptisWorks Studio

Set Part = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set OPTISViewer = CreateObject("XMPViewer.Application")

strSourceName = "source1"

strDetectorName = "LumMeter"

tempMin = 1000

tempMax = 12500

tempStep = 500

'---- For modification of the source temperature, with the part containing the source

"source1"

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

Part.OpenCompFile

Page 57: OptisWorks Automation 2014 SP1 - Optis Portal

First Macro Script Page 57 of 61

Set Part = swApp.OpenDoc6("C:\Program Files

(x86)\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings)

Set Part = swApp.ActivateDoc2("SurfaceSource", False, longstatus)

Set ModelDoc = swApp.ActiveDoc

Set Instance = Catalog.Find(ModelDoc)

Set SurfaceSource = Instance.GetSurfaceSource(strSourceName)

'---- For inverse simulation with the assembly

Set Part = swApp.ActivateDoc2("Automation.SLDASM", False, longstatus)

Set ModelDoc = swApp.ActiveDoc

Set Instance = Catalog.Find(ModelDoc)

Set InverseSimulation = Instance.GetInverseSimulation()

retval = InverseSimulation.Initialize()

For temperature = tempMin To tempMax Step tempStep

SurfaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

If Not InverseSimulation.IsSuppress Then

retval = InverseSimulation.Run()

retval = InverseSimulation.GetDetectorResultFilename(strDetectorName, strMap)

strFileOpen = strMap

OPTISViewer.OpenFile strFileOpen

strFileSave = "C:\Program Files

(x86)\OPTIS\Help\OptisWorks_Tutorials\Automation\Images\" & Format(temperature, "00000")

& ".jpg"

retval = OPTISViewer.ExportXMPImage(strFileSave, 3) '3 for JPEG format

End If

Next temperature

retval = InverseSimulation.Uninitialize()

End Sub

Page 58: OptisWorks Automation 2014 SP1 - Optis Portal

Page 58 of 61 OptisWorks Automation User Guide

SECOND MACRO SCRIPT

'---- Definitions for SolidWorks

Dim swApp As Object

Dim Part As Object

Dim SelMgr As Object

Dim boolstatus As Boolean

Dim longstatus As Long, longwarnings As Long

'---- Definitions for OptisWorks

Dim ModelDoc As ModelDoc2

Dim Catalog As OptisWorksCatalog

Dim Instance As OptisWorksInstance

Dim SurfaceSource As OptisWorksSurfaceSource

Dim InverseSimulation As OptisWorksInverseSimulation

Dim OPTISViewer As Object

Dim strSourceName As String

Dim strDetectorName As String

'---- Definitions for Optimizations

Dim XTarget As Double, YTarget As Double

Dim TemperatureInf As Double, TemperatureMid As Double, TemperatureSup As Double

Dim DeltaEInf As Double, DeltaEMid As Double, DeltaESup As Double

Dim SimuInf As Boolean, SimuMid As Boolean, SimuSup As Boolean

Sub CalcDeltaE(temperature As Double, DeltaE As Double, Simu As Boolean, strSourceName As

String)

Dim retval

Dim strMap As String

Dim strFileOpen As String

Dim Measure

If Simu = False Then

SurfaceSource.temperature = temperature

boolstatus = Part.EditRebuild3

If Not InverseSimulation.IsSuppress Then

retval = InverseSimulation.Run

retval = InverseSimulation.GetDetectorResultFilename(strDetectorName, strMap)

strFileOpen = strMap

End If

'--- Calculation of DeltaE

OPTISViewer.OpenFile strFileOpen

Measure = OPTISViewer.GetColor(0#, 0#, 1)

DeltaE = Sqr((Measure(1) - XTarget) * (Measure(1) - XTarget) + (Measure(2) - YTarget)

* (Measure(2) - YTarget))

Page 59: OptisWorks Automation 2014 SP1 - Optis Portal

Second Macro Script Page 59 of 61

Simu = True

End If

End Sub

Sub Minimize(XInf As Double, YInf As Double, UpToDateInf As Boolean, _

XMid As Double, YMid As Double, UpToDateMid As Boolean, _

XSup As Double, YSup As Double, UpToDateSup As Boolean)

If (YMid < YInf) Then ' case \

If (YMid < YSup) Then ' case \/

If (YInf < YSup) Then

XSup = XMid: YSup = YMid

XMid = (XInf + XSup) / 2: UpToDateMid = False

Else

XInf = XMid: YInf = YMid

XMid = (XInf + XSup) / 2: UpToDateMid = False

End If

Else ' case \\

XInf = XMid: YInf = YMid

XMid = XSup: YMid = YSup

XSup = XMid + (XMid - XInf): UpToDateSup = False

End If

Else ' case /

If (YMid < YSup) Then ' case //

XSup = XMid: YSup = YMid

XMid = XInf: YMid = YInf

XInf = XMid - (XSup - XMid): UpToDateInf = False

Else ' case /\

If (YInf < YSup) Then

XSup = XMid: YSup = YMid

XMid = XInf: YMid = YInf

XInf = XMid - (XSup - XMid): UpToDateInf = False

Else

XInf = XMid: YInf = YMid

XMid = XSup: YMid = YSup

XSup = XMid + (XMid - XInf): UpToDateSup = False

End If

End If

End If

End Sub

Sub main()

Dim fs, f, retval

Page 60: OptisWorks Automation 2014 SP1 - Optis Portal

Page 60 of 61 OptisWorks Automation User Guide

Set swApp = Application.SldWorks

' Set swApp = GetObject(Class:="OptisWorksStudio.Application") for OptisWorks Studio

Set Part = swApp.ActiveDoc

Set Catalog = CreateObject("OptisWorksCatalog.Catalog")

Set OPTISViewer = CreateObject("XMPViewer.Application")

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile("C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\Export.txt", True)

strSourceName = "source1"

strDetectorName = "LumMeter"

XTarget = 0.331185

YTarget = 0.340409

TemperatureInf = 1000

TemperatureSup = 12500

TemperatureMid = (TemperatureInf + TemperatureSup) / 2

SimuInf = False

SimuMid = False

SimuSup = False

'---- For modification of the source temperature, with the part containing the source

"source1"

Set SelMgr = Part.SelectionManager

boolstatus = Part.Extension.SelectByID2("SurfaceSource-1@Automation", "COMPONENT", 0, 0,

0, False, 0, Nothing, 0)

Part.OpenCompFile

Set Part = swApp.OpenDoc6("C:\Program

Files\OPTIS\Help\OptisWorks_Tutorials\Automation\SurfaceSource.SLDPRT", 1, 0, "",

longstatus, longwarnings)

Set Part = swApp.ActivateDoc2("SurfaceSource", False, longstatus)

Set ModelDoc = swApp.ActiveDoc

Set Instance = Catalog.Find(ModelDoc)

Set SurfaceSource = Instance.GetSurfaceSource(strSourceName)

'---- For inverse simulation with the assembly

Set Part = swApp.ActivateDoc2("Automation.SLDASM", False, longstatus)

Set ModelDoc = swApp.ActiveDoc

Set Instance = Catalog.Find(ModelDoc)

Set InverseSimulation = Instance.GetInverseSimulation()

retval = InverseSimulation.Initialize()

Page 61: OptisWorks Automation 2014 SP1 - Optis Portal

Second Macro Script Page 61 of 61

Do

Call CalcDeltaE(TemperatureInf, DeltaEInf, SimuInf, strSourceName)

Call CalcDeltaE(TemperatureMid, DeltaEMid, SimuMid, strSourceName)

Call CalcDeltaE(TemperatureSup, DeltaESup, SimuSup, strSourceName)

f.WriteLine TemperatureInf & " " & TemperatureSup & " " & TemperatureMid & " " & DeltaEMid

Call Minimize(TemperatureInf, DeltaEInf, SimuInf, TemperatureMid, DeltaEMid, SimuMid,

TemperatureSup, DeltaESup, SimuSup)

Loop Until (Abs(TemperatureSup - TemperatureInf) < 1)

Call CalcDeltaE(TemperatureMid, DeltaEMid, SimuMid, strSourceName)

f.WriteLine TemperatureInf & " " & TemperatureSup & " " & TemperatureMid & " " & DeltaEMid

f.Close

retval = InverseSimulation.Uninitialize()

End Sub