Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Multi layered hybrid body(Geo Set) in CATIA 1

Status
Not open for further replies.

vikt

Mechanical
Mar 13, 2013
52
Hi Friends,
Below is the code to get the hybridshapes from each geo set.
Now geo sets are inside another geo set, which is again in another geo set.


'Code
For each hb as hybridbody in part1.hybridbodies '(first level)
For each hb1 as hybridbody in hb.hybridbodies '(second level)
For each hb2 as hybridbody in hb1.hybridbodies '(third level)
'Code

My question is, how can I loop through any level of geo sets inside geo sets,
without writing or declaring hybridbody.

Regards,
Vik
 
Replies continue below

Recommended for you

google recursion

Eric N.
indocti discant et ament meminisse periti
 
it is called recursive function... basically you call the function from within the function itself :)
Sub CATMain()
dim oPart : set oPart = CATIA.ActiveDocument.Part
dim oHBodies : set oHBodies = oPart.HybridBodies
dim oHBody
for each oHBody in oHBodies
RecurseGeoSets(oHBody)
next
End Sub

function RecurseGeoSets(oHBody)
msgbox oHBody.Name
dim GeoSet2Recurse : set GeoSet2Recurse = oHBody.HybridBodies
dim oGS
for each oGS in GeoSet2Recurse
RecurseGeoSets(oGS)
next
end function


regards,
LWolf
 
need to make a function or sub that will take geoset as input and then call itself for any geoset in the input geoset

Code:
sub GeoSetRecursion (iGeoSet as HybridBody)
[indent]
if iGeoSet.HybridBodies.count>0 then

[indent]for each geoset in iGeoSet.hybridBodies
GeoSetRecursion geoset
next
[/indent]

endif

[do what you have to do][/indent]

end sub

Eric N.
indocti discant et ament meminisse periti
 
Thank you LWolf and Itsmyjob.
Learned something new today.
Will try this :)
 
Guys,
Created this macro, thought of sharing.
Let me know your feedback, any correction/optimization comment is appreciated.

'Macro to hide all hybrid shapes for any level of Geo Set
'********************************************************

Sub HideShowAllSurfaces()
Dim iCount As Integer

Dim pd1 As PartDocument
Set pd1 = CATIA.ActiveDocument

Dim part1 As Part
Set part1 = pd1.Part


For iCount = 1 To part1.HybridBodies.Count

Dim hybody1 As HybridBody
Set hybody1 = part1.HybridBodies.Item(iCount)
GeoSetRecursion hybody1

Next iCount

End Sub

Function GeoSetRecursion(iGeoSet As HybridBody)

Dim hybShape1 As HybridShape
Dim sel1 As Selection
Set sel1 = CATIA.ActiveDocument.Selection


For Each hybShape1 In iGeoSet.HybridShapes
sel1.Clear
sel1.Add hybShape1
sel1.VisProperties.SetShow catVisPropertyNoShowAttr
sel1.Clear
Next

Dim iCount1 As Integer

For iCount1 = 1 To iGeoSet.HybridBodies.Count

Dim geoset As HybridBody
Set geoset = iGeoSet.HybridBodies.Item(iCount1)
GeoSetRecursion geoset

Next iCount1

End Function
 
Hello,

There are many ways to achieve your goal but I would add some improvements to your code:
1. I use Function when I want that subroutine to return something. If it processes something by itself without the need to return anything, I use Sub.
2. Don't hide them one by one, it is unnecessary. Do something like this instead:

Code:
sel1.Clear
For Each hybShape1 In iGeoSet.HybridShapes
sel1.Add hybShape1
Next
sel1.VisProperties.SetShow catVisPropertyNoShowAttr
sel1.Clear

Of course, this idea can be extended to collecting all hybridshapes from all hybridbodies and hide them all at once.

3. I prefer For Each Next statement whenever is possible. Not all collections can be parsed like that by I find it more elegant and less lines to write.
4. Use CATIA.HSOSynchronized switch whenever possible when working with Selection . For large collection sets the improvement is huge.
5. Use meaningful naming for your objects to help you while debugging.

Having all that said, I would have written the code like that:

Code:
'Macro to hide all hybrid shapes for any level of Geo Set
'********************************************************

Sub HideShowAllSurfaces()
    
    Dim oPartDocument As PartDocument
    Set oPartDocument = CATIA.ActiveDocument
    
    Dim oSelection As Selection
    Set oSelection = oPartDocument.Selection
    oSelection.Clear
    
    Dim oPart As Part
    Set oPart = oPartDocument.Part
    
    CATIA.HSOSynchronized = False
    
    Dim oHybridBody As HybridBody
    For Each oHybridBody In oPart.HybridBodies
        GeoSetRecursion oHybridBody, oSelection
    Next
    
    oSelection.VisProperties.SetShow catVisPropertyNoShowAttr
    oSelection.Clear
    
    CATIA.HSOSynchronized = True
End Sub

Sub GeoSetRecursion(oCurrentHybridBody As HybridBody, oSelection As Selection)

    Dim oHybridShape As HybridShape
    For Each oHybridShape In oCurrentHybridBody.HybridShapes
        oSelection.Add oHybridShape
    Next

    Dim oHybridBody As HybridBody
    For Each oHybridBody In oCurrentHybridBody.HybridBodies
        GeoSetRecursion oHybridBody, oSelection
    Next

End Sub

 
Thank you so much cilici.
Exactly what i was looking for, this kind of constructive feedback is what really helps.

Without CATIA.HSOSynchronized it was taking 12-13 sec to run this macro.
With CATIA.HSOSynchronized it's now taking only 0.51 sec :) :)
Can you tell me how it works and where else i can use this ?

Thanks again.
 
Please check the API documentation (the chm file that comes with the installation of V5) for detailed explanation. I can't do it better.

This file is in bin directory.
 
Thanks cilici. Will check and explore.
Just one question.
We did this macro for one part.
I have a product with 50-100 parts in it.
Is there a way to speed up the same process when looping through each product/part in a master product ?

Regards,
Vik
 
you need to define the product, and start at that level... same way of thinking, you will need to loop through all your sub-products. right now your active document is a part, if it is a product then your code will not work.

regards,
LWolf
 
Hello,

Make sure the only document opened in CATIA is your product and run this complicated macro:

Code:
Sub ProcessAllOpenedDocuments()
    For Each oDoc In CATIA.Documents
        If TypeName(oDoc) = "PartDocument" Then Call HideShowAllSurfaces(oDoc)
    Next
End Sub

Now change the first lines of HideShowAllSurfaces like this:

Code:
Sub HideShowAllSurfaces(oDoc)
    Dim oPartDocument As PartDocument
    Set oPartDocument = oDoc
    '......
    '.....
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor