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!

Simple script help please

Status
Not open for further replies.

mad3d

Automotive
Sep 5, 2014
6
SE
Hello all

I´m a complete beginner at scripting and I can't figure out this alone.

Can someone help me with a simple script please?

I just want to select a body and run the script.
This is what the script should do:

1- Copy the body you have pre-selected
2- Paste that body with link
3- Subtract that body from the active body

Do you understand what I´m looking for, can someone help me on this?

A simple script like this would speed up my work a LOT :)

Thank you
 
Replies continue below

Recommended for you

maybe you should invest the time you will save with learning VBA?

so many example on this forum...

put some code here and I'll help - but i won't do it for you.

Eric N.
indocti discant et ament meminisse periti
 
Hi itsmyjob

Thank you for your reply.
My job doesn´t involve scripting, I just like to learn it whenever I can so that I can speed up my catia work.

I will post the code I have and maybe you can make it more abstract, because got it fomr macro recording.

Thanks so much
Paulo
 
You would need to add more detail about what you are trying to do. Is your code in VBA or CATScript/catvbs?

Are you copying from one part and pasting to another part OR are you copy/pasting within the same part?

Since you are using "with link" I assume you are pasting between parts. Here is a VBA code off the top of my head...I did not test it, but hopefully it gives you some insight. Things that seem so simple to do manually are not so simple in code. A lot more error handling and maybe interactive selection would need to be added...but here is a thought starter.

Code:
'Load a product with 2 parts
'Select published body in Part1
'Run macro
'Will link the selected body to second part and add the new body to the partbody
Sub CATMain()
	Dim oProductDocument as ProductDocument
	Dim oProduct As Product
	'Dim oPart1 As Part
	Dim oPart2 As Part
	Dim oSelection as Selection
	Dim sBodyName as String
	Dim oNewBody as Body
	Dim oShapeFactory as Factory
	Dim oRemove 'as Remove ???Not sure of the item type????
	
	On Error Resume Next 'Do not show errors
		Err.Clear 'Clear errors
		Set oProductDocument = CATIA.ActiveDocument 'Try to set the product document
		If Err.Number <> 0 then 'the open document is not a product
			Msgbox "You must open a Product to continue"
			End sub 'Closes macro
		Else
			Set oProduct = oProductDocument.Product 'Set the product variable
			Set oSelection = oProductDocument.Selection
			'Set oPart1 = oProduct.Products.Item(1).ReferenceProduct.Parent.Part
			Set oPart2 = oProduct.Products.Item(2).ReferenceProduct.Parent.Part
			Set oShapeFactory = oPart2.ShapeFactory
		End if
	
		Err.Clear 'Clear errors
		'If there is one thing selected and its type is a Body or PartBody
		If oSelection.Count = 1 and Instr(TypeName(oSelection.Item(1).Value), "Body") <> 0 then 
			'Need the name to find the body when it is pasted in Part2
			'Will not work if you have multiple bodies with the same name!
			sBodyName = oSelection.Item(1).Value.Name 
			CATIA.ActiveDocument.Selection.Copy 'Copy the body
			CATIA.ActiveDocument.Selection.Clear 'Clear the selection
			CATIA.ActiveDocument.Selection.Add oPart2 'Select Part2
			CATIA.ActiveDocument.Selection.PasteSpecial "CATPrtResult" 'Paste with link
			If Err.Number <> 0 Then 'body was not published and could not be pasted with link
				MsgBox "Body must be published to continue"
				End sub 'Closes macro
			End If
		End if
	On Error GoTo 0 'show errors
		
	'Find the new body in Part2
	Set oNewbody = oPart2.FindObjectByName sBodyName
	'When you paste the body it will be the InWorkObject
	oPart2.InWorkObject = oPart2.Bodies.Item(1) 'Set the PartBody as the In work object
	Set oRemove = oShapeFactory.AddNewRemove(oNewbody) 'add new body to part body in Part 2
	
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top