Page 1 of 1

Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 10:36 am
by berg_lauritz
Is it possible to change custom properties of a component via API/Macro with only the drawing being open?
Or do I first have to open the component in the background, modify the properties of it and then save & close it again?

Is there a resource-friendly version of opening & closing a component in the background?

Re: Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 10:55 am
by AlexLachance
I don't know how to do it, but it most certainly is doable as CustomTools, the add-on we use, does that.

Re: Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 11:19 am
by Stefan Sterk
Yes you can. I got a macro that give me the ability to change the properties of a compenent that is selected in a drawing BOM.

Re: Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 11:28 am
by berg_lauritz
Stefan Sterk wrote: Thu Sep 29, 2022 11:19 am Yes you can. I got a macro that give me the ability to change the properties of a compenent that is selected in a drawing BOM.
How do I do this i.e. from a model referenced in a drawing view?

Do I have to save the referenced document too?

Re: Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 12:20 pm
by mattpeneguy
Have you tried Property Tab Builder?

Re: Change custom properties without opening the component from the drawing?

Posted: Thu Sep 29, 2022 1:29 pm
by berg_lauritz
mattpeneguy wrote: Thu Sep 29, 2022 12:20 pm Have you tried Property Tab Builder?
one of the downfalls of the Property Tab Builder is that it does not offer the possibility to edit custom properties on components from the drawing level.

Re: Change custom properties without opening the component from the drawing?

Posted: Fri Sep 30, 2022 8:53 am
by mattpeneguy
berg_lauritz wrote: Thu Sep 29, 2022 1:29 pm one of the downfalls of the Property Tab Builder is that it does not offer the possibility to edit custom properties on components from the drawing level.
How about creating a custom BOM with the fields you want to change? Then you can edit the fields in the BOM and it would update the parts.

Re: Change custom properties without opening the component from the drawing?

Posted: Fri Sep 30, 2022 9:09 am
by berg_lauritz
Found the answer:

Code: Select all

SldWorks.View.ReferencedDocument
is what I needed.

It is meant to fix legacy parts - so making something new is not an option.


Full macro code to change/add properties to the referenced part from the first view of the active sheet of a drawing.

Code: Select all

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swView As SldWorks.View
    Dim swDrawModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    
'    Start performance enhancers
    StartNoScreenUpdate swModel
    StartLockFMT swModel
    
'    First view on the sheet (sheet format)
    Set swView = swDraw.GetFirstView
'    first actual view
    Set swView = swView.GetNextView
'    set swdrawmodel to be model that is referenced on the first sheet on the first available view
    Set swDrawModel = swView.ReferencedDocument
    
'    Exit if drawing has no referenced documents
    If swDrawModel Is Nothing Then
        MsgBox ("This drawing has no referenced documents!")
        Exit Sub
    End If

'define what should be changed
Dim propertyName As String
Dim propertyValue As String
propertyname = "some name"
propertyvalue = "some value"

'read out all title block items from drawing
AddProperty swDrawModel , propertyName, propertyValue

' End performance enhancers
    EndNoScreenUpdate swModel
    EndLockFMT swModel
    
End Sub

' adds a property with the name "propertyname" and the value "propertyvalue" to the general tab of the custom properties
Sub AddProperty(swModel As SldWorks.ModelDoc2, propertyName As String, propertyValue As String)
    Dim swModelDocExt As ModelDocExtension
    Dim swCustProp As CustomPropertyManager
    Dim newprop As Boolean

    Set swModelDocExt = swModel.Extension
    Set swCustProp = swModelDocExt.CustomPropertyManager("")

    newprop = swCustProp.Add3(propertyName, swCustomInfoText, propertyValue, swCustomPropertyReplaceValue)
End Sub

'-------------------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------------
' Speed up the macro!

Sub StartNoScreenUpdate(swModel As SldWorks.ModelDoc2)
    Dim modView As ModelView
    Set modView = swModel.ActiveView
    modView.EnableGraphicsUpdate = False
End Sub


Sub EndNoScreenUpdate(swModel As SldWorks.ModelDoc2)
    Dim modView As ModelView
    Set modView = swModel.ActiveView
    modView.EnableGraphicsUpdate = True
End Sub


Sub StartLockFMT(swModel As SldWorks.ModelDoc2)
    swModel.FeatureManager.EnableFeatureTree = False
End Sub


Sub EndLockFMT(swModel As SldWorks.ModelDoc2)
    swModel.FeatureManager.EnableFeatureTree = True
End Sub