Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Copy Partbody from product Instance

Status
Not open for further replies.

peterbeamish1

Automotive
Aug 16, 2013
13
0
0
CA
Hello,

I need to copy a partbody from an instance of a product to another product. How might I go about this?

Copying:"Removal : male"
to
"55227-8#K813 Male Plate.CATPart"

syntax may not be perfect, looking for code on how to access specific instances. copying from the document as a whole seems to give an error.
What do you recommend?

THANK YOU VERY MUCH IN ADVANCE!

Peter

CODE:

for i=1 to cavNumb

cavdesign = "55227 Cavity Design." + i

Set product3 = products2.Item(cavdesign) 'loop this

Set products3 = product3.Products

Set product4 = products3.Item("55227 Male Cavity.1")

'ERROR IS IN HERE I THINK
set product4 = product4.parts

set myPart = product4.part

set DerBODIES = myPart.Bodies

' ^i have no idea how to access an instance of a product


Set theBody = theBodies.Item("Removal : male")

selection1.add theBody

Set partDocument2 = documents1.Item("55227-8#K813 Male Plate.CATPart")

Set part2 = partDocument2.Part

selection2.Add part2

selection2.Paste

next
 
Replies continue below

Recommended for you

Just to clarify, I have many instances of a single product. I need to copy a body WITH link to another product where they will be future accessed. There are upwards of 60 children (instances) of the product within the product as a whole.

EX.
552278 Cavity Design(552278 Cavity Design.1)
552278 Cavity Design(552278 Cavity Design.2)
552278 Cavity Design(552278 Cavity Design.3)
...

Need to access each of ^ these and copy out a partbody they hold WITH link.

Peter
 
I got this far, How can i access and a partbody from oProd?



Language="VBSCRIPT"

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

Set documents1 = CATIA.Documents

Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")




'---- Begin resolution script for object : 55227 Cavity Design.1

Set productDocument1 = CATIA.ActiveDocument

Set product1 = productDocument1.Product

Set products1 = product1.Products

Set product2 = products1.Item("Cavity Group.3")

Set products2 = product2.Products

Set product3 = products2.Item("55227 Cavity Design.1")

'---- End resolution script



search(product2)





end sub



Sub Search(oProduct)
Dim Prod1
For i = 1 To oProduct.Products.Count
Set Prod1 = oProduct.Products.Item(i)
'Rename here
If Prod1.Products.Count <> 0 Then

copy(prod1)
End If
Next
End Sub


sub copy(oProd)


'msgbox oProd.name

Set bodies1 = oProd.Bodies
Set body1 = bodies1.Item("Removal : male")
body1.copy

'dfffff.PasteLink


End Sub


 
Hi,

You are using recording function as I can see, your macro is very posible not to work for other products or parts or if someone is doing a small change in the part which now have (for example) Cavity Group.3

So called "Hard code" is not the best idea (I must admit I'm using also but just for quick-dirty macros as someone said...).

Is dificult to recreate and test your scenario without some sample files (and again, I believe no one is loosing time to recreate a Product structure).

Regards
Fernando

 
The changes are not happening in cavity group. I need to copy from cavity group.
In response, this macro is necessary because otherwise I would need to copy and paste manually upwards of 60times (before doing other steps). Please examine this code:

It is working up until the copy paste section:


Language="VBSCRIPT"

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

Set documents1 = CATIA.Documents

Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")




'---- Begin resolution script for object : 55227 Cavity Design.1

Set productDocument1 = CATIA.ActiveDocument

Set product1 = productDocument1.Product

Set products1 = product1.Products

Set product2 = products1.Item("Cavity Group.3")

Set products2 = product2.Products

Set product3 = products2.Item("55227 Cavity Design.1")

'---- End resolution script






search(product2)





end sub



Sub Search(oProduct)
Dim Prod1
For i = 1 To oProduct.Products.Count
Set Prod1 = oProduct.Products.Item(i)
'Rename here
If Prod1.Products.Count <> 0 Then

copy(prod1)
End If
Next
End Sub






sub copy(oProd)

dim sel
'msgbox oProd.name


'NEED CODE HERE
Set products3 = oProd.Products
Set myObject = products3.Item("55227 Male Cavity.1")

Set HEREISIT= myObject.ReferenceProduct

set prodDoc = HEREISIT.parent

Set thepart1 = prodDoc.Part

Set bodies1 = thepart1.Bodies

Set body1 = bodies1.Item("Removal : male")

'get solids here

Set sel = prodDoc.Selection

sel.add body1

'sel.copy

CATIA.ActiveDocument.Selection.Copy()

dim selection2

Set documents11 = CATIA.Documents

Set partDocument22 = documents11.Item("55227-8#K813 Male Plate.CATPart")

Set part22 = partDocument22.Part

set selection2 = partDocument22.selection

selection2.Add part22

'selection2.PasteLink
'selection2.Paste

'THIS GIVES AN ERROR
CATIA.ActiveDocument.Selection.PasteLink()

'part11.PasteLink


End Sub
 
Thank you for your replies

I am attaching a sample file of our companies file structure. This document should be used as an example, only to better understand what needs to be accomplished.

The macro should take:

-Cavity Group (Cavity Group.3)
----SAMPLE Cavity Design(55227 Cavity Design.X) <-This product will be cycled in a for loop.
---------55227 Male Cavity (55227 Male Cavity.1)
---------------Removal : male

COPY TO:
-SAMPLE Male Plate(Male1)


At the end, SAMPLE Male Plate Should Contain many "Removal : Male" bodys



Thank you
 
 http://files.engineering.com/getfile.aspx?folder=c9677082-b029-4c4d-b8f5-126980582157&file=SAMPLE.rar
Hi

One solution could be like in code below in CATScript, is done just for one body, you need to adapt the code for the others....or find another solution, for example to copy all bodies in one shot.

Code:
Language="VBSCRIPT"

Sub CATMain()

Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = productDocument1.Selection

selection1.Clear 

''''''''''''''get the part from where you copy the body
Dim documents1 As Documents
Set documents1 = CATIA.Documents

Dim partDocument1 As Document
Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")

Dim part1 As Part
Set part1 = partDocument1.Part

Dim bodies1 As Bodies
Set bodies1 = part1.Bodies

Dim body1 As Body
Set body1 = bodies1.Item("Removal : male")

selection1.Add body1
selection1.Copy  '''' copy Removal : male

''''''''''''''activate the target part
Dim selection2 As Selection
Set selection2 = productDocument1.Selection
selection2.Search "Name='SAMPLE Male Plate',all"   ''''''select by searching the name

        Set partDocument1 = documents1.Item("55227-8#K813 Male Plate.CATPart")

        Dim editPart
        Set editPart = partDocument1
        CATIA.StartWorkbench("PrtCfg")
        editPart.Activate()'    '''''make the part active (highlight - blue color)
	
	selection1.PasteSpecial "CATPrtResult"  ' ----- Perform Paste As Result With Link

End Sub

Regards
Fernando

 
Thanks again fernado,

I don't think my main problem is being communicated correctly. I have working copy and paste code, but what I don't understand is how to copy specific items (I.e SAMPLE Cavity Design 55227 Cavity Design.X) and NOT the document itself. My "Search" subroutine loops through each product I need and calls the copy routine with the correct part. Inside sub copy how do I select and copy prod (and NOT partDocument1)?

NOTE: Different names are used in this code (not SAMPLE) but the tree structure is identical.

Code:
Language="VBSCRIPT"

Sub CATMain()

Set productDocument1 = CATIA.ActiveDocument

Set documents1 = CATIA.Documents

Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")




'---- Begin resolution script for object : 55227 Cavity Design.1

Set productDocument1 = CATIA.ActiveDocument

Set product1 = productDocument1.Product

Set products1 = product1.Products

Set product2 = products1.Item("Cavity Group.3")

Set products2 = product2.Products

Set product3 = products2.Item("55227 Cavity Design.1")

'---- End resolution script


search(product2)



end sub


'*************************************************************************************************
Sub Search(oProduct) 'THIS SUB RETURN EACH PRODUCT 
    Dim Prod1
         For i = 1 To oProduct.Products.Count
            Set Prod1 = oProduct.Products.Item(i)
            'Rename here
            If Prod1.Products.Count <> 0 Then
              
			copy(prod1)
			msgbox prod1.name
            End If
        Next
End Sub

'**************************************************************************************************
sub copy(oProd)

Dim productDocument1
Set productDocument1 = CATIA.ActiveDocument

Dim documents1
Set documents1 = CATIA.Documents

Dim partDocument1
Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")

Dim part1
Set part1 = partDocument1.Part

Dim bodies1
Set bodies1 = part1.Bodies

Dim body1
Set body1 = bodies1.Item("Removal : male")



Dim selection1
Set selection1 = productDocument1.Selection

selection1.Add body1
selection1.Copy


paste

end sub
'*************************************************************************************************

'*************************************************************************************************
sub paste

Set productDocument1 = CATIA.ActiveDocument
Set documents1 = CATIA.Documents
Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")

Dim selection2
Set selection2 = productDocument1.Selection

'selection2.Search "Name='SAMPLE Male Plate',all"   ''''''select by searching the name
selection2.Search "Name='55227-8#K813 Male Plate',all"   ''''''select by searching the name

Set partDocument1 = documents1.Item("55227-8#K813 Male Plate.CATPart")

        Dim editPart
        Set editPart = partDocument1
        'CATIA.StartWorkbench("PrtCfg")
        editPart.Activate()
	
'	selection1.PasteSpecial "CATPrtResult"  ' ----- Perform Paste As Result With Link

'CATIA.ActiveDocument.Selection.PasteLink()
CATIA.ActiveDocument.Selection.Paste


End Sub


Regards,

Peter
 
You cannot copy a CATProduct in a CATPart. What I understood you want to copy some bodies from a CATPart inside another one, no matter where they are in CATProduct, this is what you can do.

Hope you understood the hierarcy in a Product structure. You can copy what ever you want by making the parts active (blue highlight) and make the target active and paste there (and this can be done under CATProduct or in separate windows...).

Regards
Fernando

 
Okay this makes more sense.

So within the sub search, activate the specific part before trying to copy from it?
Code:
Sub Search(oProduct) 'THIS SUB RETURN EACH PRODUCT 
    Dim Prod1
         For i = 1 To oProduct.Products.Count
            Set Prod1 = oProduct.Products.Item(i)
            'Rename here
            If Prod1.Products.Count <> 0 Then
              
'ACTIVATE CODE HERE	<-------------------------------------------------------------------		
copy(prod1)
			'msgbox prod1.name
            End If
        Next
End Sub

What should i be activating? The product or the part? If part, how to I select that specific one?

thanks
 
Just do some operations manually and record a macro, see what is recording and try to correlate with what you've done.

So, if you want to copy something from a Part, of course you need to get it...remember? ......Item("55227 Male Cavity.CATPart"). You can get it also by Instance Name...

Read a little bit more carefully the macro posted and try also to read about this in v5automation.chm file...you have a lot of things to learn when you start to do something like this.

Regards
Fernando

 
Thank you for your post
I understand the basics of copy from a part. This particular case required you to copy from a instance of a part within an instance of a product. I understand the product structure, but am hung up on selecting a body within two instances. Here is the section of code that needs work:

Code:
Sub Search(oProduct) 'THIS SUB RETURN EACH PRODUCT 
    Dim Prod1
         For i = 1 To oProduct.Products.Count
            Set Prod1 = oProduct.Products.Item(i)
            'Rename here
            If Prod1.Products.Count <> 0 Then
             

			copy(prod1)
			'msgbox prod1.name
'prod1.activate()
            End If
        Next
End Sub

'**************************************************************************************************
sub copy(oProd)

'Dim productDocument1
'Set productDocument1 = CATIA.ActiveDocument

set trythis = oProd.products


'Dim documents1
'Set documents1 = CATIA.Documents


'Dim partDocument1
'Set partDocument1 = documents1.Item("55227 Male Cavity.CATPart")



Dim selection1
Set selection1 = productDocument1.Selection


Set oDoc = CATIA.ActiveDocument
Set oProd1 = oDoc.Product
Set oProds1 = oProd1.Products
set oProds11 = oProds1.item("Cavity Group.3")
Set oMyDoc = oProds11.products
set newT = oMyDoc.item(oProd.name)
'set newnewT = newT.products
'set bbodies1 = newT.Bodies

set newnewT = newT.ReferenceProduct

NumberOfItems = newnewT.Products.Count
Dim bodies1
Dim body1

For k = 1 To NumberOfItems  

Set maybe = newnewT.Products.Item(k)
'msgbox maybe.name

if  maybe.name = "55227 Male Cavity.1" then

		set secndRef = maybe.referenceProduct
		
		
			
		secndRef.activate()	'error here, the following lines of code have errors as well. How do i select the particular_ 'instance?		

	set part1 =secndRef.part


	Set bodies1 = part1.Bodies

	Set body1 = bodies1.Item("Removal : male")


end if


next

selection1.Add body1
selection1.Copy


paste

end sub
 
Status
Not open for further replies.
Back
Top