Change custom properties without opening the component from the drawing?
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Change custom properties without opening the component from the drawing?
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?
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?
Found the answer:
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.
Go to full postCode: Select all
SldWorks.View.ReferencedDocument
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
- AlexLachance
- Posts: 2322
- Joined: Thu Mar 11, 2021 8:14 am
- Location: Quebec
- x 2521
- x 2134
Re: Change custom properties without opening the component from the drawing?
I don't know how to do it, but it most certainly is doable as CustomTools, the add-on we use, does that.
- Stefan Sterk
- Posts: 45
- Joined: Tue Aug 10, 2021 2:40 am
- x 67
- x 85
Re: Change custom properties without opening the component from the drawing?
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.
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Change custom properties without opening the component from the drawing?
How do I do this i.e. from a model referenced in a drawing view?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.
Do I have to save the referenced document too?
- mattpeneguy
- Posts: 1386
- Joined: Tue Mar 09, 2021 11:14 am
- x 2489
- x 1899
Re: Change custom properties without opening the component from the drawing?
Have you tried Property Tab Builder?
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Change custom properties without opening the component from the drawing?
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.
- mattpeneguy
- Posts: 1386
- Joined: Tue Mar 09, 2021 11:14 am
- x 2489
- x 1899
Re: Change custom properties without opening the component from the drawing?
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 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.
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Change custom properties without opening the component from the drawing?
Found the answer:
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
SldWorks.View.ReferencedDocument
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