Custom Property

Hello.

I would like to modify one or two custom properties of a part (contained in my assembly without opening it)  

The goal would be  to control all this from an extel macro. 

I only found this one that works correctly: Part.CustomInfo("No_article) = 200020

But you have to open the room :/ .

Could you help me

Hello

You can edit the custom properties of a SolidWorks document without opening it by using the Document Manager APIs. These are the APIs used by applications such as MyCadTools.

Personally, I have created an application that links and modifies the properties of the components of an assembly in order to manage the bills of materials and ensure the link with our ERP.

To do this, you must apply for a license in your customer area.

Then here are some examples.

Good code...

2 Likes

Hello

What is it that you don't want to open in SW? your part or your assembly?

look by ICI, the demand looks a little (or even a lot).

Kind regards

1 Like

Thank you for your answer, I don't want to open my parts, my assembly would be  active.

And yes D.Roger  I try to be inspired by this person but I still have some worries.

Dim swApp As Object

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

Sub Modif_art2()

'Set swApp = _
'Application.SldWorks

'Set Part = swApp.ActiveDoc
Set swApp = CreateObject("SldWorks.Application")
Set swModelDoc = swApp.ActiveDoc

boolstatus = Part.Extension.GetModelDoc2("XXXXXD06-1@XXXXX_630S_chgt_D_bavette-1", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.CustomInfo("No_article") = 200


' works in a room
'boolstatus = Part.AddCustomInfo3("", "No_article", 1, "")
'Part.CustomInfo("No_article") = 2000200
End Sub
 

But here is my problem right now.

Runtime error 91 

Object variable or Bloc With variable not defined

Hello

You create a Solidworks object: Set swApp = CreateObject("SldWorks.Application")

You get the current document: Set swModelDoc = swApp.ActiveDoc

But between the 2 you would have to open a document in Solidworks otherwise you try to recover an object that doesn't exist, hence the message "object not defined" because I guess in debug mode it's on this line that it freezes.

Kind regards

1 Like

My assembly would be active, it would be generated and then I would call this dialog box, so it would only be necessary to

Set swModelDoc = swApp.ActiveDoc

but it always tells me variable object or block not defined :/

Excuse me for my little expertise at this level^^

I was able to advance a little more on the subject 

 

Dim swApp As Object

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

Sub Modif_art2()
Set swApp = CreateObject("SldWorks.application") '_
Set Part = swApp.ActiveDoc
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------


'------------------------------------------------------------------------------------------------------------------------------------------------------------------------
swApp.ActivateDoc2 "xxxxx.SLDASM", False, longstatus
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.GetModelDoc2("XXXXXD06-1@XXXXX_630S_chgt_D_bavette-1", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.CustomInfo("No_article") = 2003700


End Sub

 

Runtime error 438 (on the bold line)

Property or method not supported by this object 

 

Yes, this is normal since the GetModelDoc2 function is a function belonging to the Icomponent2 objects, whereas in your code you try to call it through an IModelDoc2 object.

At the same time, you opened another question that can be found HERE but it seems to be the same question, which one should be answered?

You can find an example of how to use the GetModelDoc2 function HERE.

Kind regards

1 Like

Thank you for your answer I opened this question to rephrase my question I find that I had expressed myself badly this is surely not very useful sorry .

Then I don't really understand what is the point (1.-1)

Debug.Print "Selected Type =" & swSelMgr. GetSelectedObjectType3 (1, -1)

 

Look HERE in the "Parameters" paragraph, and don't hesitate to do some research (on each function used) in the help on the Solidworks APIs, these are very well documented...

Kind regards

1 Like

Dim swApp As Object

Dim Part As Object
Dim BoolStatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sun swSelMgr As SldWorks.SelectionMgr
Dim swSelObj As Object
Dim swSelComp As SldWorks.Component2
 Dim swEnt As SldWorks.Entity


Sub Modif_art2()

 Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
Set swSelComp = swSelMgr.GetSelectedObjectsComponent3(1, -1)

Set swSelComp = swEnt. XXXXXD06(name of my piece)

        Part.CustomInfo("No_article") = 20037
End Sub

 

This blocks in the name of my parts I made a bad manipulation?

Execution error '91'

Object variable or Bloc With variable not defined

 

Dim feature As feature

Dim boolstatus As Variant

Dim Part As Object

Dim longstatus As Long, longwarnings As Long

Sub Modif_art2()


Set swApp = CreateObject("SldWorks.Application")

Set Model = swApp.ActiveDoc

 

' Select the feature named "Extrude1"
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Boss.-Extru.1@XXXXXD06-1@XXXXX_0000_00_D_111", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

 

' If the selection was successful, that is, "Extrude1" was

' selected and it is a "BODYFEATURE", then get that feature; otherwise,

' indicate failure

If boolstatus = True Then

    Sun SelMgr As SelectionMgr

    Set SelMgr = Model.SelectionManager

    Set feature = SelMgr.GetSelectedObject6(1, 0)
    
    
    
Set swCustPropMgr = swCutlistItem.CustomPropertyManager("No_article")

Debug.Print "No_article"

Debug.Print "Number of custom properties = 28 " + CStr(swCustPropMgr.Count("6"))

Debug.Print "No_article", "", "200", "Text"

 

'Names = swCustPropMgr.GetNames("2000")


End If

End Sub*

 

I also tried this method and I'm stuck on the line:

Debug.Print "Number of custom properties = 28 " + CStr(swCustPropMgr.Count("6"))

with an "Item Required" error

Ref : http://help.solidworks.com/2012/English/api/sldworksapi/Get_Custom_Properties_for_Cut-list_Item_Example_VB.htm

in order to see if I managed to access the ownership of my piece.

 

 

Hello

I'm sorry to tell you this but I can't even read your code because the objects and functions are mixed up without taking into account their characteristics and functioning. Aren't you trying to learn about Solidworks macros by skipping the steps?

Kind regards

2 Likes

I'm a bit lost for this vba I don't really know how to go about it

Before thinking about VBA Solidworks I think that you should already take an interest in the programming language itself in order to understand how it works, there are a whole bunch of courses that can already help to start like for example HERE or HERE, yes it's for Excel but it doesn't matter the philosophy is the same: object-oriented programming. Then and only then can you be interested in the Solidworks-specific APIs on the online help directly here.

At the beginning it takes time and it may seem a bit tedious but that's at the beginning...

Kind regards

2 Likes

Okay it would be useful to me I will try to repropose a version after study of all this thank you