Could someone share a few pointers on how to go about creating an API (using VB) to manipulate (and create) custom properties for part models and assembly models. I have programming experience so a reference to the specific methods and properties of the part object model would suffice.
I have a bunch of part models to which I wish to add a few custom properties and values to those properties. I hope to be able to open each model make the changes and close the model.
While I'm at it, is it possible to open a bunch of drawings and refresh the sheet layout programatically?
Foraging through the help, although interesting and informative, can be time comsuming. This project has been dumped on my lap and there is a time crunch. So I wonder if the API approach can save me some time.
Custom properties can be quickly manipulated programatically, if they aren't Configuration specific, using DSOfile.dll you may have to download it if you don't have it on your machine.
here is some sample code that i wrote to update MaterialNo and Material (which in my case is the description). In this case I used a list of part numbers in excel tried to change the props and returned a string for the status and also changed the cell color for the status.
Function ChangeSldWorksMaterial(PartNo As String, CurrentCell As Range) As String 'written by Corey Scheich in December of 2004 'Opens a part file using company directory standards 'Atempts to change the custom properties of the file 'If successful changes the cell to green and returns that both props were changed 'if unsuccessful changes the cell to red or yellow depending on where the mishap was ChangeSldWorksMaterial = "Failed" 'code to get description Dim PropReader As DSOleFile.PropertyReader Dim ThisFile As SldWorks.ModelDoc2 Dim AllProps As DSOleFile.CustomProperties Dim FileDesc As DSOleFile.CustomProperty Dim RdOnly As Boolean Dim Count As Long Dim FilePath As String Dim FoundMaterialNo As Boolean Dim FoundMaterial As Boolean Dim Prop As DSOleFile.CustomProperty
FoundMaterial = False FoundMaterialNo = False
'use company standard model folder structure developed from part number FilePath = "G:\Models\" & Left(PartNo, 3) & "\" & PartNo & ".sldprt"
Set PropReader = CreateObject("DSOleFile.PropertyReader") RdOnly = True On Error Resume Next RdOnly = PropReader.GetDocumentProperties(FilePath).IsReadOnly On Error GoTo 0 Select Case RdOnly Case True 'if file is read only change cell to red and return the text below CurrentCell.Interior.Color = RGB(255, 0, 0) ChangeSldWorksMaterial = "Part was Read Only" Exit Function
Case False Set AllProps = PropReader.GetDocumentProperties(FilePath).CustomProperties Count = 1 For Count = 1 To AllProps.Count 'compare the names of available custom properties 'to desired names non case sensitive Select Case UCase(AllProps.Item(Count).Name) Case UCase("Description") strDescription = AllProps.Item(Count).Value Case UCase("Material#") 'set the new value AllProps.Item(Count).Value = "500090" FoundMaterialNo = True Case UCase("Material") 'set the new value for multi line AllProps.Item(Count).Value = "HRSHT 10GA" & vbCrLf & _ "(.135t)" & vbCrLf & _ "72 X 120" FoundMaterial = True
End Select
Next 'check if each prop was found If FoundMaterialNo = False Then Set Prop = AllProps.Add("Material#", "500090") If Prop Is Nothing Then GoTo NotSet End If If FoundMaterial = False Then Set Prop = AllProps.Add("Material", "HRSHT 10GA" & vbCrLf & _ "(.135t)" & vbCrLf & _ "72 X 120") If Prop Is Nothing Then GoTo NotSet End If
CurrentCell.Interior.Color = RGB(0, 255, 0) End Select Set PropReader = Nothing Set AllProps = Nothing ChangeSldWorksMaterial = "Both properties changed successfully" Exit Function NotSet: 'if any props weren't found set the color to yellow and return the string below CurrentCell.Interior.Color = RGB(255, 130, 0) ChangeSldWorksMaterial = "Could not add one of the properties" End Function
To refresh the sheet layout on a single sheet drawing you should only have to open and save it. To refresh it on a multi sheet drawing you may have to open each sheet. If you want to do alot of files I would suggest running it at night as it will be very taxing on your computer while running as I am sure you could guess.
PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.