• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

select the parts of a set with vba

  • Thread starter Thread starter Adahm
  • Start date Start date

Adahm

Guest
Does anyone know if you can select a part of a multi-level set in vba?
with ilogic there is the .select command with the following syntax:

odocdef.selectset.select (oocc)

where odocdef is the axieme and oocc is the component of the axieme to be selected.
But there are two problems.
  1. I can't find the same command in vba
  2. the command selects only the components of the first level.
It seems that in the odocdef it serves the complete path from the first group that contains the second and so on, but I do not know how to do it.

the code I used is as follows:
Code:
syntaxeditor code snippetsub main()
dim assydoc as assemblydocument
dim compdef as componentdefinition
dim compoccurrence as componentoccurrence

assydoc = thisapplication.activedocument

'start exploration from active axieme
explorecomponents (assydoc.componentdefinition)
end

'recurring function to explore the hierarchy of components
sub explorecomponents(compdef as componentdefinition)
dim i as integer
for i = 1 to compdef.occurrences.count
compoccurrence = compdef.occurrences.item(s)
if compoccurrence.definitiondocumenttype = 12291 then ' iscomponentdefinition then
' if it is a defined component (can contain other components)
explorecomponents (compoccurrence.definition)
♪
' messagebox.show("part selected: " & compoccurrence.name)
call partselection (compdef, compoccurrence)
end if
next i
end

sub partselection (odef, oocc)
dim odocdef as document
odocdef = odef.document
dim odococc as document
odococc = oocc.definition.document

dim opropset as propertyset
opropset = odococc.propertysets.item("inventor user defined properties")

dim oprop as [Property]
    on error resume next
    oprop = opropset.item("foratura")
    
    if oprop.value = "sì" then
        odocdef.selectset.select (oocc)
    end if
end sub
in my case only the parts that have certain characteristics, but that is a detail.
I attach a small set of examples for those who didn't have one to do two tests.
in the file together001 there is already the code shown above.
file version: inventor 2023.

Thank you.
 

Attachments

vba code;
I moved the definition of the selectset to the first level and passed the reference to the recurring subs, to the nose every time you redefine a selectset in the sub "died there" like the other local variables, but they are running and I did not see well.
Anyway, I thought it worked, let me know.

(note: that correction of syntax for vba)

Code:
public sub testselectadham()
    ' imposta il riferimento al documento attivo.
    ' questo presuppone che il documento attivo sia un assieme
    dim odoc as inventor.assemblydocument
    set odoc = thisapplication.activedocument
    
    ' ottiene la definizione del componente d'assieme
    dim ocompdef as inventor.componentdefinition
    set ocompdef = odoc.componentdefinition

    dim oselectset as selectset
    set oselectset = odoc.selectset
    
    dim smsg as string
    dim ileafnodes as long
    dim isubassemblies as long
    
    ' definisci il nome della iproperty e il valore da cercare
    dim ipropertyname as string
    ipropertyname = "foratura" '"trattamento"
    dim ipropertyvalue as string
    ipropertyvalue = "sì" '"colorare"
    
    ' ottiene tutte le occorrenze dalla definizione del componente per il documento di tipo assembly
    dim ocompocc as componentoccurrence
    for each ocompocc in ocompdef.occurrences
        ' controlla se è un'occorrenza figlia (nodo foglia)
        if ocompocc.suboccurrences.count = 0 then
            msgbox ("padre:" & ocompocc.name)
            call partselection(ocompdef, ocompocc, ipropertyname, ipropertyvalue, oselectset)
            ileafnodes = ileafnodes + 1
        else
            msgbox (ocompocc.name)
            isubassemblies = isubassemblies + 1
            call processallsubocc_adahm(ocompocc, _
                                smsg, _
                                ileafnodes, _
                                isubassemblies, _
                                ipropertyname, _
                                ipropertyvalue, _
                                oselectset)
                                
        end if
    next
    
    msgbox ("n. di nodi foglia    : " + cstr(ileafnodes))
    msgbox ("numero di sottogruppi: " + cstr(isubassemblies))
end sub

' questa funzione viene chiamata in modo ricorsivo per l'elaborazione dei sottoassiemi
' per iterare l'intero albero degli assiemi.
private sub processallsubocc_adahm(byval ocompocc as componentoccurrence, _
                             byref smsg as string, _
                             byref ileafnodes as long, _
                             byref isubassemblies as long, _
                             byref ipropname as string, _
                             byref ipropval as string, _
                             byref oselectset as selectset)
    
    dim osubcompocc as componentoccurrence
    for each osubcompocc in ocompocc.suboccurrences
        ' controlla se è un'occorrenza figlia (nodo foglia)
        if osubcompocc.suboccurrences.count = 0 then
            msgbox ("figlio: " & osubcompocc.name)
            call partselection(ocompocc.definition, osubcompocc, ipropname, ipropval, oselectset)
            ileafnodes = ileafnodes + 1
        else
            smsg = smsg + osubcompocc.name + vbcr
            isubassemblies = isubassemblies + 1

            call processallsubocc_adahm(osubcompocc, _
                                  smsg, _
                                  ileafnodes, _
                                  isubassemblies, _
                                  ipropname, _
                                  ipropval, _
                                  oselectset)
        end if
    next
end sub

sub partselection(odef, oocc, ipropname, ipropval, oselectset as selectset)
    dim odocdef as document
    set odocdef = odef.document
    dim odococc as document
    set odococc = oocc.definition.document
    
    msgbox (odocdef.displayname & " - " & odococc.displayname & " - (partselection)")
    
    ' ottieni il selectset attivo
    'dim oselectset as selectset
    'set oselectset = odocdef.selectset
    msgbox ("select set: " & oselectset.count)
    
    dim opropset as propertyset
    set opropset = odococc.propertysets.item("inventor user defined properties")
    
    dim oprop as property
    'on error resume next
    set oprop = opropset.item(ipropname)
    'messagebox.show(">" & oprop.name & ": " & oprop.value & "<")
    
    if oprop.value = ipropval then
        msgbox (">" & oprop.name & ": " & oprop.value & "<" & vbcrlf & "dovrebbe selezionare :" & odococc.displayname)
        'messagebox.show("sono passato di qui, quindi significa che oprop.vaule = ipropval e adesso devo selezionare la parte")
        call oselectset.select(oocc)
    end if
end sub
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top