API / VBA for retrieving custom properties of a component of an assembly

Hi all

I'm currently working on a project to automate some folders and I'd like to integrate some features into my existing macro, which inserts and exports a BOM in an assembly in TXT format.

I thank those who will take the time to read me, and to give me a few moments to unblock myself. :)

The export and save part of the BOM is going well, but I have a problem when I try to retrieve the values of the properties of one of the components (the only one to have these properties): CLIENT, LOCAL, CPLT LOCAL, DEAL, DRAFTSMAN, QTE ASS) before copying them under the same name = value in the assembly.

I had tinkered with something by "manipulating" the exported Excel table based on:

AFFAIRE = Range("BG2:BG1000").Find("*", , , , xlByColumns).Value            'Cherche la première valeur non vide
On Error Resume Next                                            

 

But this only works if the property does exist. In short, I would like to find a more precise and safe solution.

I've searched and tried a lot of bits of code and/or solutions found on the site or elsewhere, but I really don't know enough about APIs and VBA code to adapt it to my needs.

However, I have identified some interesting functions, of which here is an example below to simply read the CODE properties in a MsgBox. (that would already be a good step:) but unfortunately doesn't work)  

 Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swSelComp               As Variant
    Dim swSelModel              As SldWorks.ModelDoc2
    Dim swCustPropMgr           As CustomPropertyManager
    Dim swEnt                   As SldWorks.Entity
    Dim val                     As String
    Dim valout                  As String
 
   
    Set swSelComp = swBOMAnnotation.GetComponents2(2, Configuration)
    
    Set swCustPropMgr = swSelComp.Extension.CustomPropertyManager("")
    
    swCustPropMgr.Get4 "CODE", False, val, valout 'Change property name here
    MsgBox "Value: " & val & "Evaluated value: " & valout

 

The final idea would therefore be to retrieve the component variable, copy it into the assembly, and then into each of the parts with the properties CODIFICATION ="X"

Do you have any advice and obvious corrections to share with me in order to unblock me? I thank you very much in advance. Do not hesitate to ask me for more details.

I'm attaching my two pieces of code, one of which is really rough and used for my research / test... The second is the simplest one that works when the properties are entered.

PS: I should point out that I don't have the myCADtools available.

Thank you all.

 

Michael                              


test_export_txt_nomenclature.swp

And here's the second "simpler" snippet of code that works when properties are entered.:)


enregistrement_nomenclature.swp

Hello

start by modifying your "enregistrement_nomenclature" macro by putting the line "On Error Resume Next" at the beginning of the code (just below your "Sub main()" line) and no need to put several, this line is an error handling command, no link with any loop, look at it here .

Kind regards

1 Like

For point 2: to be able to modify the properties of the parts you must actually use the GetComponents2 function and then for each component use the GetModelDoc2 function to be able to work on these properties.

Kind regards

1 Like

Hello

I had indeed come across a lot of your answers during my research, I was counting on your intervention. :)

And thank you very much, I just moved my "On Error Resume Next" function to the beginning, and then also add:

If Cells(I + 1, J + 1) = " " Then
        Cells(I + 1, J + 1).ClearContents
        End If

  since after the export of the properties the cells are not empty but all contain a space (" "). I guess it disturbed the rest of my macro... especially the:

AFFAIRE = Range("BG2:BG1000").Find("*", , , , xlByColumns).Value            'Cherche la première valeur non vide

 

On the other hand, I'd still like to find a way to extract the custom properties of the components directly via    GetComponents2 and then  GetModelDoc2 , but for that I'll have to continue my research and probably ask for a little help;)

Thank you a thousand times for unblocking me from this first case in any case. :)

 

Very good end of the day to you,

 

Kind regards

 

Mickael