×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

CATPart or CATProduct Copy & Paste Question

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

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

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 --> 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 Sub 

Eric N.
indocti discant et ament meminisse periti

RE: CATPart or CATProduct Copy & Paste Question

(OP)
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! winky smile Thank you!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources