 |  | How to Set Part Density and Custom PropertiesTutorial Level = Intermediate by Greg Jankowski, SolidWorks Corporation This VB (Visual Basic) application sets the part density and custom properties based on values entered in a spreadsheet. This eliminates the need for multiple part templates for each material type. This application consists of two parts: a module (PartDensity1) which defines and reads the density and custom property information, and UserForm1 which provides the UI (User Interface) and sets the new values. Features: - Read density value and custom property (Material).
- Automatically add the custom properties if they do not exist (Material).
- Works for any unit of measure.
Requirements: - Only set the density for a part. Warn the user if it's a drawing or assembly.
- Work with different units of measure. Only change the density if it is supported within the application.
- Check to see if the user selected a value and entered a number for the density, if required. It is a good programming practice to check to see if the user entered a valid value. Otherwise, when you try to set the density to "ABC," the application will have a problem.
Challenges: - All values for density are in kg/m^3. So the macro needs to convert the default value in the current unit of measure.
- Set the material type from a pull-down menu. If the property existed, use that name.
Key: Text = Comments Text = Reserved VB Words Text = Non-reserved words PartDensity1 ' **************************************************************** ' Name: SetPartDensity.swp Ver: 1.0 ' Created By: gjankowski@solidworks.com ' SolidWorks Ver: 2001 Plus ' Description: This macro will set the part density based on ' user input. ' ****************************************************************
Dim swApp As Object 'SldWorks object Dim Part As Object 'ModelDoc object Public pDensity, materialName Public cUnits As Long 'Consistent with type names defined in swconst.bas Const swDocPART = 1 Const swMaterialPropertyDensity = 7
Sub main()
'Get the active session Set swApp = CreateObject("SldWorks.Application")
'Get handle to the active SolidWorks part Set Part = swApp.ActiveDoc
'Set the message box props Dim Msg, Style, Title, Help, Ctxt, Response, MyString Title = "Set Part Density" ' Define the title
'If not a part, then exit If (Part.GetType <> swDocPART) Then 'Define the message Msg = "Current document is not a part. Density is only set on a part." 'Display the message box Response = MsgBox(Msg, 0, Title) Exit Sub End If
'Get the current UOM cUnits = Part.LengthUnit
'Get the current part density in (kg/meter^3) pDensity = Part.GetUserPreferenceDoubleValue(swMaterialPropertyDensity)
'Convert the UOM if it is not Meters. The current application only supports 'the listed UOM's. Additional UOM's can be added If cUnits = 0 Then 'mm 'Sets the value from the default value and converts the UOM pDensity = pDensity / 1000000 'Set the text value for the UOM UserForm1.txtUOM.Caption = "gram/mm^3" ElseIf cUnits = 1 Then 'cm pDensity = pDensity / 1000 UserForm1.txtUOM.Caption = "gram/cm^3" ElseIf cUnits = 2 Then 'm pDensity = pDensity / 1 UserForm1.txtUOM.Caption = "gram/m^3" ElseIf cUnits = 3 Then 'in pDensity = pDensity / 27679.9125177614 UserForm1.txtUOM.Caption = "lb/in^3" ElseIf cUnits = 4 Then 'ft pDensity = pDensity / 16.0184678922231 UserForm1.txtUOM.Caption = "lb/in^3" ElseIf cUnits = 5 Then 'ft-in pDensity = pDensity / 27679.9125177614 UserForm1.txtUOM.Caption = "lb/in^3" Else 'If the current UOM is not supported, do not set the value and exit Msg = "The current UOM is not supported!" 'Set the message Response = MsgBox(Msg, 0, Title) 'Display the message box End End If
'This section defines the material pull-down list 'When adding to this list, the corresponding values must 'be added to the UserForm1 module where the density is set.
UserForm1.frmMatName.AddItem "ALUMINUM 2024-T3" UserForm1.frmMatName.AddItem "ALUMINUM 3003" UserForm1.frmMatName.AddItem "ALUMINUM 6061-T6" UserForm1.frmMatName.AddItem "ALUMINUM 7079-T6" UserForm1.frmMatName.AddItem "COPPER - PURE" UserForm1.frmMatName.AddItem "IRON" UserForm1.frmMatName.AddItem "NICKEL -PURE" UserForm1.frmMatName.AddItem "NYLON" UserForm1.frmMatName.AddItem "POLYCARBONATE" UserForm1.frmMatName.AddItem "POLYETHYLENE" UserForm1.frmMatName.AddItem "PTFE" UserForm1.frmMatName.AddItem "STEEL AISI 304" UserForm1.frmMatName.AddItem "STEEL AISI 4130" UserForm1.frmMatName.AddItem "STEEL AISI C1020" UserForm1.frmMatName.AddItem "TITANIUM B 120VCA"
'Populate the dialog box density value field UserForm1.frmDValue.Value = pDensity
'Get the property "MATERIAL"" exists materialName = Part.CustomInfo("MATERIAL")
'if the material prop does not exist, prompt the user to select one If materialName = "" Then materialName = "Select a material from the list" End If
'Use this value for the material name in the UserForm dialog boxUserForm1.frmMatName = materialName
'Set the focus on the input dialog box, and select text field so the 'user can just enter the number UserForm1.frmDValue.SetFocus UserForm1.frmDValue.SelStart = 0 UserForm1.frmDValue.SelLength = 90
'Show the input dialog box UserForm1.Show
End Sub
UserForm1 ' **************************************************************** ' Name: SetPartDensity.swp Ver: 1.0
Sub btnCancel_Click() 'Exit the application if the cancel button is pressed End
End Sub
Sub btnOK_Click()
'Consistent with type names defined in swconst.bas Const swMaterialPropertyDensity = 7 Const swCustomInfoText = 30
'Declare the other variable names Dim wasSet, retVal As Boolean
Set swApp2 = CreateObject("SldWorks.Application") Set part2 = swApp2.ActiveDoc
'Get the value from the density value field pDensity = UserForm1.frmDValue.Value
'If the value was not a number warn the user and make them enter a number If Not IsNumeric(pDensity) Then 'it a number Msg = "The density value must be a number" 'Display message. Response = MsgBox(Msg, 0, Title) End End If
'If a new material was selected, re-set the density value 'Only do this if the material has changed.
If PartDensity1.materialName <> UserForm1.frmMatName Then 'All values in kg/m^3 'When adding to this list, the corresponding values must 'be added to the PartDensity1 module where the material pull-down 'list is defined. If UserForm1.frmMatName = "ALUMINUM 2024-T3" Then pDensity = 2770 ElseIf UserForm1.frmMatName = "ALUMINUM 3003" Then pDensity = 2700 ElseIf UserForm1.frmMatName = "ALUMINUM 6061-T6" Then pDensity = 2700 ElseIf UserForm1.frmMatName = "ALUMINUM 7079-T6" Then pDensity = 2740 ElseIf UserForm1.frmMatName = "COPPER - PURE" Then pDensity = 8900 ElseIf UserForm1.frmMatName = "IRON" Then pDensity = 7830 ElseIf UserForm1.frmMatName = "NICKEL -PURE" Then pDensity = 8900 ElseIf UserForm1.frmMatName = "NYLON" Then pDensity = 1700 ElseIf UserForm1.frmMatName = "POLYCARBONATE" Then pDensity = 1300 ElseIf UserForm1.frmMatName = "POLYETHYLENE" Then pDensity = 2300 ElseIf UserForm1.frmMatName = "PTFE" Then pDensity = 1200 ElseIf UserForm1.frmMatName = "STEEL AISI 304" Then pDensity = 8030 ElseIf UserForm1.frmMatName = "STEEL AISI C1020" Then pDensity = 7850 ElseIf UserForm1.frmMatName = "TITANIUM B 120VCA" Then pDensity = 4850
'If the user does not select a value, pick Not Defined and use 'the value shown
ElseIf UserForm1.frmMatName = "Select a material from the list" Then UserForm1.frmMatName = "NOT DEFINED" pDensity = UserForm1.frmDValue Else 'If the user does not select a new material, use the density 'value shown 'This allows to the user to enter a different density value 'that the default pDensity = UserForm1.frmDValue End If End If
'Remove and re-apply the property retVal = part2.DeleteCustomInfo("MATERIAL") retVal = part2.AddCustomInfo2("MATERIAL", swCustomInfoText, UserForm1.frmMatName)
'Convert the UOM back to grams/m^3. Do not have to check for unsupported 'UOMs because it was checked already
If cUnits = 0 Then 'mm pDensity = pDensity * 1000000 ElseIf cUnits = 1 Then 'cm pDensity = pDensity * 1000 ElseIf cUnits = 2 Then 'm pDensity = pDensity * 1 ElseIf cUnits = 3 Then 'in pDensity = pDensity * 27679.9125177614 ElseIf cUnits = 4 Then 'ft pDensity = pDensity * 16.0184678922231 ElseIf cUnits = 5 Then 'ft-in pDensity = pDensity * 27679.9125177614 End If 'Set the density in the SolidWorks part. This method will return true 'if the value was set correctly wasSet = part2.SetUserPreferenceDoubleValue(swMaterialPropertyDensity, CDbl(pDensity))
'Turn off the user form UserForm1.Hide
'Define the message and format the density value and display the system 'unit of measure
If wasSet Then 'If the value was set ok Msg = "Part Density has been set to -> " & Format(pDensity) & " kg/cubic meter" Else Msg = "There was a problem setting the density value" End If 'Display message. Response = MsgBox(Msg, 0, Title)
'Release the objects Set Part = Nothing Set part2 = Nothing Set swApp = Nothing Set swApp2 = Nothing
'After the value has been set, exit the app End
End Sub
|
To view this macro see Set Part Density.swp Related Articles
|