CATPart or CATProduct Copy & Paste Question
CATPart or CATProduct Copy & Paste Question
(OP)
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
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 --> VBSCript
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 




RE: CATPart or CATProduct Copy & Paste Question
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 --> CATVBA
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 Subindocti discant et ament meminisse periti
RE: CATPart or CATProduct Copy & Paste Question
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!