简体   繁体   中英

Changing text on a solidworks part

I'm trying to change some text I have in a solidworks part using a macro. I thought my best bet was to set the text to equal a custom property, and then change the value of that property using vba.

The closest I seem to have got is this

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Part.FileSummaryInfo
End Sub

Which is just taken from a recorded macro. As far as I can see, it just selects the file properties, without changing anything.

Anyone know how I would make this work? Or if there's a different way to achieve the same thing?

thanks

Try using the CustomPropertyManager object. In the API help there's an example similar to what you're after: https://help.solidworks.com/2020/english/api/sldworksapi/Get_Custom_Properties_for_Configuration_Example_VB.htm

You didn't say what version of SW you're on but the API should be similar enough across many software versions.

I'd also recommend using strong types in place of "Object". Ie change "Dim swApp As Object" to "Dim swApp As SldWorks.SldWorks", etc. This allows the VBA editor to suggest methods/properties.

This code works to update the custom property. As mentioned in a comment, it's limitation is that the text which is linked to the custom property does NOT update until it is opened. I don't know code which will open the text.

    Dim swApp As SldWorks.SldWorks

    Dim swModel As SldWorks.ModelDoc2

Dim swCustPropMgr As SldWorks.CustomPropertyManager

Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Code As String

 Sub setcode()

Code = "test2"

End Sub

Sub main()

setcode

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

swCustPropMgr.Add3 "Texttesting", swCustomInfoText, Code, swCustomPropertyReplaceValue


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM