Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Copy & Paste published elements in context 2

Status
Not open for further replies.

mrawlinc

Industrial
Nov 19, 2012
36
SI
I have one level assembly with multiple parts and their instances on different positions. Parts already have published elements (lines and axis systems) that are needed to be copied in (empty) part called "Positions" which is in the in same assembly, so the position of copied elements doesn't change, and also their name should be same as in source part.
I have very very basic skills on programming, so I searched for a similar macro, but couldn't find it ... does anyone have something similar that I could use for a start?
 
Replies continue below

Recommended for you

i would suggest to develop your programming skills one step at a time.

first make a script to identify the 'active document' and make sure it s a product, if not message the user and exit

Second script would be to add an new part in your product with the proper name.

Third script would be to find the first part in your assembly and identify (select) the element to copy.

Fourth would be the copy / paste.

Fifth would be the keep the name requirement.

Finally modify your script to navigate the assembly for the source part.

show us your progress and tell us the challenge you have, you'll find many people ready to help.

Many user could learn from this, so don't be shy to share.

before you do that, go check the v5automation.chm file on your catia install folder (and read as many script as you can) and the catia portable script center (and read more script) ...

Waiting for your code of step 1 and 2, this should not be too long.


Eric N.
indocti discant et ament meminisse periti
 
Here is code, handling first two steps ...

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
   End If
        Debug.Print rootProduct.Name & " Is Product!"
   Dim newPart As Product
   Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
End Sub

Now I'm a bit confused how to properly set search parameters, so it would find and select only published elements in parts
 
Good.

Before you get to the publication, you should get the first part.

check in your rootProduct.Products

tree_anhpyq.png
vbawatch_g0infw.png


Once you have the proper object, you'll find the publications

Eric N.
indocti discant et ament meminisse periti
 
I hope I understood correctly, as you can see in code below, I used For loop to run trough all parts and their instances in assembly, and for now just output part's names when loop is running(for check).
Additional question at this point is, if I can limit parts used in FOR loop to only visible parts?

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions5")
   
    Dim subProduct As Product
    For i = 1 To rootProduct.Products.Count
        Set subProduct = rootProduct.Products.Item(i)
        Debug.Print rootProduct.Products.Item(i).Name
    Next   
End Sub
 
your FOR loop will go from 1 to rootProduct.Products.Count visible or not

What you can do is check if rootProduct.Products.Item(i) is visible or not

the visibility is given by selection.VisProperties

CATIA.ActiveDocument.Selection is the selection, once you define the selection object you can add, remove, copy, paste...

Can you debug.Print only visible instances?

Eric N.
indocti discant et ament meminisse periti
 
what I'm trying now is to filter all parts inside FOR loop with IF, if current part is visible or not, but I'm heaving a bit trouble to test this VisProperties correctly, as you will notice in my code:
Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
    Dim subProduct As Product
    For i = 1 To rootProduct.Products.Count
        Set subProduct = rootProduct.Products.Item(i)
        If subProduct.VisProperties = True Then
            Debug.Print subProduct.Name & " IS visible!"
        Else
            Debug.Print subProduct.Name & " is NOT visible!"
        End If
    Next
End Sub
 
VisProperties belongs to selection object not to product object

Eric N.
indocti discant et ament meminisse periti
 
check v5automation.chm about selection.visproperty

Eric N.
indocti discant et ament meminisse periti
 
ok it's a bit tricky the first time so I'll give you that one

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
    Dim oSel As Selection
    Dim vp As VisPropertySet
    
    
    For i = 1 To rootProduct.Products.Count
        Set Instance = rootProduct.Products.Item(i)
        If isVisible(Instance) Then
            Debug.Print (Instance.Name & " is visible")
        Else
            Debug.Print (Instance.Name & " is NOT visible")
        End If
    Next
End Sub

Function isVisible(ByRef object As Variant) As Boolean
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add object
    
    Dim vp As VisPropertySet
    Dim showstate As CatVisPropertyStatus
    Set vp = CATIA.ActiveDocument.Selection.VisProperties
    vp.GetShow showstate
    
    result = True
    
    If showstate = catVisPropertyNoShowAttr Then result = False
    
    CATIA.ActiveDocument.Selection.Clear
    
    isVisible = result
End Function

I create a function that return true or false is the element I give to the function is visible.

so first step I loop the product for subproduct (instances)

I get the instance object and give it to the function

the function clear the selection, then add the instance to the selection, then check its visibility and finally return the Boolean

Ok now you have to select the element you want to copy.


Eric N.
indocti discant et ament meminisse periti
 
There is also something tricky about the copy paste in context with VBA

Manually, if the "blue level" is the root product you cannot copy geometry elements but only product elements (instances...)

you have to make the part (not the instance) the "blue level" in order to do the copy/paste.

Well in VBA you do the copy paste with the root product as "blue level"

that's the ay it is.

Waiting for your code...


Eric N.
indocti discant et ament meminisse periti
 
I managed to implement show/hide state test in this way:
Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
    Dim subProduct As Product
    Dim oSel As Selection
    Set oSel = CATIA.ActiveDocument.Selection
    oSel.Clear
    
    For i = 1 To rootProduct.Products.Count
        Set subProduct = rootProduct.Products.Item(i)
        oSel.Add rootProduct.Products.Item(i)
        
        Dim showstate As CatVisPropertyShow
        Set visProperties1 = CATIA.ActiveDocument.Selection.VisProperties
        visProperties1.GetShow showstate

        If showstate < 1 Then
            Debug.Print subProduct.Name & " IS visible!"
        Else
            Debug.Print subProduct.Name & " is NOT visible!"
        End If
        oSel.Clear
        
        Next      
End Sub
comments please, if there should be something done differently...
 
Would be better to use visibility test as function or subroutine like you did? I guess I will need to use selection for selecting elements that I need to copy from parts and instances?
 

yep you need selection for copy / paste but it's not incompatible.

did you notice that your positions.1 instance is in the loop? how can you solve that?

Eric N.
indocti discant et ament meminisse periti
 
Here is another IF loop added comparing names to added part called Positions in my case ...

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
    Dim oSel As Selection
    Dim vp As VisPropertySet
    
    
    For i = 1 To rootProduct.Products.Count
        Set instance = rootProduct.Products.Item(i)
        If isVisible(instance) Then
            If instance.Name = newPart.Name Then
                Debug.Print (instance.Name & " This is Positions part!")
            Else
            Debug.Print (instance.Name & " IS visible")
            End If
        Else
            Debug.Print (instance.Name & " is NOT visible")
        End If
    Next
End Sub

Function isVisible(ByRef object As Variant) As Boolean
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add object
    
    Dim vp As VisPropertySet
    Dim showstate As CatVisPropertyStatus
    Set vp = CATIA.ActiveDocument.Selection.VisProperties
    vp.GetShow showstate
    
    result = True
    
    If showstate = catVisPropertyNoShowAttr Then result = False
    
    CATIA.ActiveDocument.Selection.Clear
    
    isVisible = result
End Function
 
that's one solution.

I would have made the count of product before adding the position part like :

Code:
nbOfInstanceBeforeAddingPart = rootProduct.Products.Count

dim newpart...
set newpart...

for i = 1 to nbOfInstanceBeforeAddingPart

Can you post a picture of the tree structure showing the publication and eventually the geometry to be copied?

Did you manage to select your geometry? is you geometry always the same name in the same geometricalset? or the only way to find the geometry is from the publication?

Eric N.
indocti discant et ament meminisse periti
 
Here is code with part and instances count before adding new part called Positions:
Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim PsrtsCount As Integer
    PartsCount = rootProduct.Products.Count
    
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
   
    Dim oSel As Selection
    Dim vp As VisPropertySet
    
    
    For i = 1 To PartsCount
        Set instance = rootProduct.Products.Item(i)
        If isVisible(instance) Then
            If instance.Name = newPart.Name Then
                Debug.Print (instance.Name & " This is Positions part!")
            Else
            Debug.Print (instance.Name & " IS visible")
            End If
        Else
            Debug.Print (instance.Name & " is NOT visible")
        End If
    Next
End Sub

Function isVisible(ByRef object As Variant) As Boolean
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add object
    
    Dim vp As VisPropertySet
    Dim showstate As CatVisPropertyStatus
    Set vp = CATIA.ActiveDocument.Selection.VisProperties
    vp.GetShow showstate
    
    result = True
    
    If showstate = catVisPropertyNoShowAttr Then result = False
    
    CATIA.ActiveDocument.Selection.Clear
    
    isVisible = result
End Function

Here is Tree structure of my parts, and all parts are created from template part (new from), and in this case it already has published elements like axis system and line. It is important that copied axis system has same name as source part.
Capture_1_odtvg3.jpg
 
file from a temple is a good idea, it will make the selection easier...

your code still don't do the selection of the element for copy...it should not be that hard...

Eric N.
indocti discant et ament meminisse periti
 
For selecting publications, I first count them and then make selection trough FOR loop, I hope I access to publications of each part correctly, please comment if it should be done differently ...

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim PartsCount As Integer
    PartsCount = rootProduct.Products.Count
        
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")

    Dim vp As VisPropertySet
        
    For i = 1 To PartsCount
        Set instance = rootProduct.Products.Item(i)
        If isVisible(instance) Then
            Debug.Print (instance.Name & " IS visible")
            Dim publCount As Integer
            publCount = rootProduct.Products.Item(i).Publications.Count
            
            Dim selectedEL As Selection
            Set selectedEL = CATIA.ActiveDocument.Selection
            selectedEL.Clear
                                    
            For j = 1 To publCount
                Set publishedEL = rootProduct.Products.Item(i).Publications.Item(j)
                selectedEL.Add publishedEL
                Debug.Print (rootProduct.Products.Item(i).Publications.Item(j).Name & " published element is added to selection")
            Next
        Else
            Debug.Print (instance.Name & " is NOT visible")
        End If
    Next
End Sub

Function isVisible(ByRef object As Variant) As Boolean
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add object
    
    Dim vp As VisPropertySet
    Dim showstate As CatVisPropertyStatus
    Set vp = CATIA.ActiveDocument.Selection.VisProperties
    vp.GetShow showstate
    
    result = True
    
    If showstate = catVisPropertyNoShowAttr Then result = False
    
    CATIA.ActiveDocument.Selection.Clear
    
    isVisible = result
End Function
 
I'm trying to add also copy/paste of selected elements, and I have a bit of a problem "targeting" my newPart correctly to paste elements in it

Code:
Sub CATMain()
    Dim rootProduct As Product
    Set rootProduct = CATIA.ActiveDocument.Product
    
    If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
        Debug.Print rootProduct.Name & " Is NOT Product!"
    Exit Sub
    End If
    
    Debug.Print rootProduct.Name & " Is Product!"
    
    Dim PartsCount As Integer
    PartsCount = rootProduct.Products.Count
        
    Dim newPart As Product
    Set newPart = rootProduct.Products.AddNewComponent("Part", "Positions")
    

    Dim vp As VisPropertySet
        
    For i = 1 To PartsCount
        Set instance = rootProduct.Products.Item(i)
        If isVisible(instance) Then
            Debug.Print (instance.Name & " IS visible")
            Dim publCount As Integer
            publCount = rootProduct.Products.Item(i).Publications.Count
            
            Dim selectedEL As Selection
            Set selectedEL = CATIA.ActiveDocument.Selection
            selectedEL.Clear
                                    
            For j = 1 To publCount
                Set publishedEL = rootProduct.Products.Item(i).Publications.Item(j)
                selectedEL.Add publishedEL
                Debug.Print (rootProduct.Products.Item(i).Publications.Item(j).Name & " published element is added to selection")
                'Debug.Print newPart.Name
                selectedEL.Copy
                selectedEL.Clear
                selectedEL.Add newPart
                selectedEL.PasteSpecial ("CATPrtResultWithOutLink")
                selectedEL.Clear
            Next
        Else
            Debug.Print (instance.Name & " is NOT visible")
        End If
    Next
End Sub

Function isVisible(ByRef object As Variant) As Boolean
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add object
    
    Dim vp As VisPropertySet
    Dim showstate As CatVisPropertyStatus
    Set vp = CATIA.ActiveDocument.Selection.VisProperties
    vp.GetShow showstate
    
    result = True
    
    If showstate = catVisPropertyNoShowAttr Then result = False
    
    CATIA.ActiveDocument.Selection.Clear
    
    isVisible = result
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top