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!

CATPart or CATProduct Copy & Paste Question

Status
Not open for further replies.

tpalsl125

Automotive
Apr 24, 2015
44
DE
Hallo, all

I think this script is very simple but it doesn't work as what I want.

I recoreded the macro to copy and paste a cataprt or catproduct.

And I modified a little bit like below script.

To copy "Part1" in "Product1" works very well.

But it doesn't work in "Product2".

I uploaded the picture of tree to attatched file.

I want to copy and paste a CATPart or CATProduct in every product.

Could you check my script?!
Link
Code:
Language="VBSCRIPT"

Sub CATMain()


Dim Inputobject(0)
Dim Selection(1)

Set productDocument1 = CATIA.ActiveDocument
Set selection1 = productDocument1.Selection
selection1.Clear 
Set product1 = productDocument1.Product
Set products1 = product1.Products

MsgBox "Please, Select a Product for linked part."
Set Selection(0) = CATIA.ActiveDocument.Selection
Selection(0).Clear
Inputobject(0) = "Product"
Status = Selection(0).SelectElement2(Inputobject, "Select a Product", True)
SelObj1 = Selection(0).Item(1).Value.Name
'Set product2 = products1.Item("Product1.1")
Set product2 = products1.Item(SelObj1)
Set products2 = product2.Products

MsgBox "Please, Select a Part to copy."
Set Selection(1) = CATIA.ActiveDocument.Selection
Selection(1).Clear
Inputobject(0) = "Product"
Status = Selection(1).SelectElement2(Inputobject, "Select a Product", True)
SelObj2 = Selection(1).Item(1).Value.Name
'Set product3 = products2.Item("Part1.3")
Set product3 = products2.Item(SelObj2)
selection1.Add product3

selection1.Copy
Set productDocument1 = CATIA.ActiveDocument
Set selection2 = productDocument1.Selection
selection2.Clear 
selection2.Add product2
selection2.Paste 


End Sub
 
 http://files.engineering.com/getfile.aspx?folder=8314a6e5-d482-4894-8afd-90f5319f5b85&file=111.jpg
Replies continue below

Recommended for you

strange code...

why selection(0) and selection(1) ???

Also I would replace:

SelObj1 = Selection(0).Item(1).Value.Name
'Set product2 = products1.Item("Product1.1")
Set product2 = products1.Item(SelObj1)
Set products2 = product2.Products

by:

set TargetProduct = Selection(0).Item(1).Value

Same way I'll use :

set sourcePart = Selection(0).Item(1).Value

and finally

with selection
.clear
.add sourcePart
.copy
...
end with


Final code:
Code:
Sub CATMain()

    Dim Inputobject(0)
    Dim oSel 'As Selection
    
    Set productDocument1 = CATIA.ActiveDocument
    Set oSel = productDocument1.Selection
    oSel.Clear
    
    MsgBox "Please, Select a target product to receive Part."
    oSel.Clear
    Inputobject(0) = "Product"
    Status = oSel.SelectElement2(Inputobject, "Select a Product", True)
    Set targetProduct = oSel.Item(1).Value
    
    MsgBox "Please, Select a Part to copy."
    oSel.Clear
    Status = oSel.SelectElement2(Inputobject, "Select a Product", True)
    Set sourcePart = oSel.Item(1).Value
    
    With oSel
        .Clear
        .Add sourcePart
        .Copy
        .Clear
        .Add targetProduct
        .Paste
        .Clear
    End With

End Sub


Eric N.
indocti discant et ament meminisse periti
 
Thank you so much, itsmyjob!!

Your answer saved me today!! That is absolutly what I want!!

Fist I thought I need two selections. One is "Target Product", the other is "Part to copy".

This is reason why I use selection(1) before knowing "wiht" method..

I haven't seen the "with" method before..

I think you must be prodigy! ;-) Thank you!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top