×
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

Simple script help please

Simple script help please

Simple script help please

(OP)
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 smile

Thank you

RE: Simple script help please

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

RE: Simple script help please

(OP)
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

RE: Simple script help please

why just not to subtract without copy/paste?

RE: Simple script help please

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

'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 

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