Change custom properties without opening the component from the drawing?

Programming and macros
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Change custom properties without opening the component from the drawing?

Unread post 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?
by berg_lauritz » Fri Sep 30, 2022 9:09 am
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
Go to full post
User avatar
AlexLachance
Posts: 2141
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2305
x 1974

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

Unread post 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.
User avatar
Stefan Sterk
Posts: 36
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 74

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

Unread post 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.
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

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

Unread post 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?
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

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

Unread post by mattpeneguy »

Have you tried Property Tab Builder?
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

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

Unread post 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.
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

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

Unread post 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.
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

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

Unread post 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
Post Reply