Document Properties
Upon successful completion of this lesson, you will be able to:
I Use the API to change system options and document properties.
I Locate and automate checkboxes, textbox values, listboxes, radio buttons and slider bars within the SolidWorks options dialog.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
User Preferences - System Options 83
User
Preferences - System Options
In order to programatically change system options in SolidWorks, it is necessary to call specific user preference methods. These methods allow a program to change individual settings found within the Tools, Options menu. These methods are:
SldWorks::SetUserPreferenceToggle SldWorks::SetUserPreferenceIntegerValue SldWorks::SetUserPreferenceDoubleValue SldWorks::SetUserPreferenceStringValue
Note Each method listed above also has a counterpart.
Substitute SetUserPreference with GetUserPreference to retrieve existing system options.
Setting Checkboxes
Call the SldWorks::SetUserPreferenceToggle method to turn on and off checkbox items within the SolidWorks options dialog.
SldWorks::SetUserPreferenceToggle
SldWorks.SetUserPreferenceToggle (UserPreferenceValue, onFlag)
Input: UserPreferenceValue See UserPreference Tables For System Options on page 92.
Toggle items are designated with “Toggle”.
Input: onFlag TRUE = toggle the item on
FALSE = toggle the item off
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
1 Create a new macro.
Click the New Macro button on the Macro toolbar.
Name the macro SystemOptions.swp.
2 Early bind to SolidWorks.
Edit code to reflect early binding behavior.
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks End Sub
3 Add code to change toggle values.
Automate the first four checkboxes in the System Options, General page.
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks swApp.SetUserPreferenceToggle _
swConst.swInputDimValOnCreate, True swApp.SetUserPreferenceToggle _
swConst.swSingleCommandPerPick, True swApp.SetUserPreferenceToggle _
swConst.swShowDimensionNames, True swApp.SetUserPreferenceToggle _
swConst.swShowErrorsEveryRebuild, True End Sub
4 Save and run the macro.
Test to see if the checkboxes are enabled.
Return to VBA when finished.
Tip In many cases an option may only need to be changed when the macro is run. It is polite to set a changed option to the value it was set before the macro runs. An end user may become irritated if they have to manually change the option back to their preference every time they run the macro.
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
User Preferences - System Options 85
Setting Textboxes with Integers
Call the SldWorks::SetUserPreferenceIntegerValue method to enter values for textbox items that require integer values.
5 Add code to change viewport color.
On the Colors page, change the Viewport Background color value.
Dim swApp As SldWorks.SldWorks Dim viewportColor As Long Sub main()
Set swApp = Application.SldWorks swApp.SetUserPreferenceToggle _
swConst.swInputDimValOnCreate, True swApp.SetUserPreferenceToggle _
swConst.swSingleCommandPerPick, True swApp.SetUserPreferenceToggle _
swConst.swShowDimensionNames, True swApp.SetUserPreferenceToggle _
swConst.swShowErrorsEveryRebuild,True
viewportColor = RGB(128, 255, 128) ‘sets color to green swApp.SetUserPreferenceIntegerValue _
swConst.swSystemColorsViewportBackground, viewportColor End Sub
6 Save and run macro.
Test to see if the textbox values have changed.
Return to VBA when finished.
SldWorks::SetUserPreferenceIntegerValue
retval = swApp.SetUserPreferenceIntegerValue (UserPreferenceValue, value)
Output: retval TRUE if the successful, FALSE if not.
Input: UserPreferenceValue See UserPreference Tables For System Options on page 92.
Integer value items are designated with “Int”. Input: value Numeric value that you want to give to the user
preference specified in userPreferenceValue
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
Setting Textboxes with Doubles
Call the SldWorks::SetUserPreferenceDoubleValue method to enter values for textbox items that require numeric values with decimal points.
7 Add code to change the detail view scale.
On the Drawings page, change the Detail View Scaling value.
Set swApp = Application.SldWorks swApp.SetUserPreferenceToggle _
swConst.swInputDimValOnCreate, True swApp.SetUserPreferenceToggle _
swConst.swSingleCommandPerPick, True swApp.SetUserPreferenceToggle _
swConst.swShowDimensionNames, True swApp.SetUserPreferenceToggle _
swConst.swShowErrorsEveryRebuild,True
color = RGB(128, 255, 128) ‘sets color to green swApp.SetUserPreferenceIntegerValue _
swConst.swSystemColorsViewportBackground, color swApp.SetUserPreferenceDoubleValue _
swConst.swDrawingDetailViewScale, 1.5 End Sub
8 Save and run macro.
Test to see if the textbox value has changed then return to VBA.
SldWorks::SetUserPreferenceDoubleValue
retval = swApp.SetUserPreferenceDoubleValue (UserPreferenceValue, value)
Output: retval TRUE if the successful, FALSE if not.
Input: UserPreferenceValue See UserPreference Tables For System Options on page 92.
Double value items are designated with “Double”. Input: value Numeric value that you want to give to the user
preference specified in userPreferenceValue
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
User Preferences - System Options 87
Setting Textboxes with String Values
Call the SldWorks::SetUserPreferenceStringValue method to enter values for textbox items that require characters.
9 Add code to change backup directory string value.
On the Backups page, change the Save Backup Copies in Directory string value.
Dim value As String Sub main()
Set swApp = Application.SldWorks swApp.SetUserPreferenceToggle _ color = RGB(128, 255, 128) ‘sets color to green swApp.SetUserPreferenceIntegerValue _
10 Save and run macro.
Test to see if the textbox value has changed then return to VBA.
SldWorks::SetUserPreferenceStringValue
retval = swApp.SetUserPreferenceStringValue (UserPreferenceValue, value)
Output: retval TRUE if the successful, FALSE if not.
Input: UserPreferenceValue See UserPreference Tables For System Options on page 92.
String value items are designated with “String”. Input: value Numeric value that you want to give to the user
preference specified in userPreferenceValue
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
Setting Listboxes Call the SldWorks::SetUserPreferenceIntegerValue; StringValue or Toggle method (for lists that have only two choices) to change listbox values.
Setting Radio Buttons
Call the SldWorks::SetUserPreferenceIntegerValue or Toggle method to change radio button values. Toggle is sometimes used for radio buttons with only two options.
Setting Slider Bars Call the SldWorks::SetUserPreferenceIntegerValue or DoubleValue method to move slider bars.
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
User Preferences - System Options 89
11 Add code to change a radio button and slider bars.
On the Display/Selections page, change the Hidden Edges Displayed As option to Solid.
On the View Rotation page, change the Mouse Speed value and the View Animation Speed value.
Dim swApp As SldWorks.SldWorks Dim color As Long
Dim value As String
Sub main()
Set swApp = Application.SldWorks swApp.SetUserPreferenceToggle _
color = RGB(128, 255, 128) ‘sets color to green swApp.SetUserPreferenceIntegerValue _
‘ View Rotation - Mouse Speed
‘ 0 = Slow
‘ 100 = Fast
swApp.SetUserPreferenceIntegerValue _ swConst.swViewRotationMouseSpeed, 50
‘ View Rotation - ViewAnimationSpeed
‘ 0 = Off
12 Save and run macro.
Test to see if the radio button changed and the two slider bars moved.
13 Exit macro.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
User
Preferences - Document Properties
In order to customize or automate default document properties in SolidWorks, it is necessary to connect to both the SldWorks object and the ModelDoc2 object. Then call the same user preference methods as before but make the call to ModelDoc2, instead of to SldWorks.
ModelDoc2::SetUserPreferenceToggle ModelDoc2::SetUserPreferenceIntegerValue ModelDoc2::SetUserPreferenceDoubleValue ModelDoc2::SetUserPreferenceStringValue
The following table highlights the ability to set a checkbox for document property using ModelDoc2, not SldWorks.
1 Create a new part.
2 Create a new macro.
Click the New Macro button and name the macro DocumentProperties.swp.
3 Early bind to SolidWorks.
Edit code to reflect early binding behavior.
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks End Sub
ModelDoc2::SetUserPreferenceToggle
ModelDoc2.SetUserPreferenceToggle (UserPreferenceValue, onFlag)
Input: UserPreferenceValue See UserPreference Tables For System Options on page 92.
Toggle items are designated with “Toggle”.
Input: onFlag TRUE = toggle the item on
FALSE = toggle the item off
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
Locating the Correct APIs and Enumeration Values 91
4 Connect to ModelDoc2.
Add the following lines of code:
Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Sub main()
Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc End Sub
5 Add code to change toggle value.
On the Document Properties, Detailing page enable the checkbox for Dual Dimension Display.
Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc swModel.SetUserPreferenceToggle _
swConst.swDetailingDualDimensions, True End Sub
6 Create a new part in SolidWorks.
Save and run the macro.
Test to see if the checkbox is enabled.
Locating the Correct APIs and
Enumeration Values
It is often difficult to determine which APIs and enumerations values to use in the API help file. A helpful way to determine the settings for a particular option, is to record a new macro and change the setting. Then edit the macro to find out what APIs were used and what values were passed to it.
1 Record a new macro.
Click the Record button from the Macro toolbar 2 Change an option.
Click Tools, Options... from the menu and click View Rotation on the System Options tab.
Set the Mouse speed and the View animation speed to Fast. Click OK to accept the settings.
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
3 Change the options again.
Again, click Tools, Options... from the menu and click View Rotation on the System Options tab.
Set the Mouse speed and the View animation speed to Slow. Click OK to accept the settings.
4 Study the code to determine the settings.
Save the macro anywhere you like and edit the code.
The maximum and minimum values are recorded along with the appropriate APIs for changing the settings.
Sub main()
Set swApp = Application.SldWorks
‘Fast value of Mouse Speed
swApp.SetUserPreferenceIntegerValue _ swViewRotationMouseSpeed, 100
‘Fast value of View Rotation Speed
swApp.SetUserPreferenceDoubleValue swViewAnimationSpeed, 0.5
‘Slow value of Mouse Speed
swApp.SetUserPreferenceIntegerValue swViewRotationMouseSpeed, 0
‘Slow value of View animation speed.
swApp.SetUserPreferenceDoubleValue swViewAnimationSpeed, 3 End Sub
UserPreference Tables For System Options
The following tables list all of the System Options and how they are represented by the API.
The tables have three columns that show the:
I Setting in the System Options to be changed.
I Type of user preference method used to change the setting.
I Enumerations / Values used when changing the options.
Most of the options are supported by the API, however some are not.
The Enumeration / Value column will display Not Supported if the setting cannot be changed by the API.
Note Some settings can be set on both the SldWorks and the ModelDoc2 object. An example would be the Mouse Speed that was shown in the previous case study. Study the User Preference enumeration values in the API help file for more information.
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
UserPreference Tables For System Options 93
General
Setting Type Enumeration / Values
Open Last Used Document Toggle swOpenLastUsedDocumentAtStart Input dimension value Toggle swInputDimValOnCreate
Single command per pick Toggle swSingleCommandPerPick Show dimension names Toggle swShowDimensionNames Show errors every rebuild Toggle swShowErrorsEveryRebuild Maximize document on open Toggle swMaximizeDocumentOnOpen Use shaded face highlighting Toggle swUseShadedFaceHighlight Show thumbnail graphics in Windows Explorer Toggle swThumbnailGraphics
Use system separator for dimensions Toggle swUseSystemSeparatorForDims Use English Language menus Toggle Not Supported
Use English language feature and file names Toggle Not Supported
Enable performance email Toggle swEnablePerformanceEmail Enable Confirmation Corner Toggle swEnableConfirmationCorner Auto-show PropertyManager Toggle swAutoShowPropertyManager Automatically edit macro after recording Toggle Not Supported
Custom property used as component description String swCustomPropertyUsedAsComponentDescription
Pr e-R elea se
Do no t co py or dis trib ute
Lesson 3 API Fundamentals
Drawings
Setting Type Enumeration / Values
Automatically place dimensions from model Toggle swDrawingAutomaticModelDimPlacement Automatically scale new drawing views Toggle swAutomaticScaling3ViewDrawings
Show contents while dragging drawing view Toggle swDrawingViewShowContentsWhileDragging Smooth dynamic motion of drawing views Toggle swDrawingViewSmoothDynamicMotion Display new detail circles as circles Toggle swDrawingCreateDetailAsCircle Select hidden entities Toggle swDrawingSelectHiddenEntities
Eliminate duplicate model dimensions on insert Toggle swDrawingEliminateDuplicateDimsOnInsert Allow auto-update when opening drawings Toggle swAutomaticDrawingViewUpdateDefault Detail item snapping when dragging corner Toggle Not Supported
Detail item snapping when dragging center Toggle Not Supported
Print out-of-sync water mark Toggle swRapidDraftPrintOutOfSynchWaterMark Show reference geometry names in drawings Toggle swShowRefGeomName
Automatically hide components on view creation Toggle swDrawingViewAutoHideComponents Display sketch arc center points Toggle swDisplayArcCenterPoints Display sketch entity center points Toggle swDisplayEntityPoints Save tessellated data for drawings with shaded and
draft quality views
Toggle swDrawingSaveShadedData
Print breaklines in broken view Toggle swDrawingPrintBreaklinesInBrokenView Print out-of-date drawing views with crosshatch Int swDrawingPrintCrosshatchOutOfDateViews Detail view scaling Double swDrawingDetailViewScale
Custom property used as Revision String swDrawingCustomPropertyUsedAsRevision Keyboard movement increment Not Supported
Pr e-R elea se
Do no t co py or dis trib ute
API Fundamentals Lesson 3
UserPreference Tables For System Options 95
Display Style
Setting Type Enumeration / Values
Display style for new views Int swHiddenEdgeDisplayDefault swDisplayMode_e
Tangent edges in new views Int swTangentEdgeDisplayDefault swDisplayTangentEdges_e
Display quality for new views Toggle swDrawingsDefaultDisplayTypeFastHLRHLV swDrawingsDefaultDisplayTypeHLREdgesWhenSha