Hi Georg,
The API does not provide a functionality like that directly.
However, you can create a new instance which is based on the same definition as the existing occurrence, then set its transformation to whatever you need.
Cheers,
Hi Adam,
Do you have an example for copy " 2 parts constrained" and paste in the same assembly.
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Cristiano Oliveira
ConsultCAD.com
I use this to copy a sketch to a workplane in a part document. But my guess is it could also be used to copy parts.
Dim Part1 As part component = ...
Dim Part2 As part component = ...
paste?
' Select sketch Dim oSketchToCopy As PlanarSketch = oDef.Sketches.Item("Vorm_C1") Call oDoc.SelectSet.Clear() Call oDoc.SelectSet.Select(oSketchToCopy) ' copy sketch Dim oCopyControlDef As ControlDefinition = oInvApp.CommandManager.ControlDefinitions.Item("AppCopyCmd") oCopyControlDef.Execute() ' Select workplane for paste sketch Call oDoc.SelectSet.Clear() Call oDoc.SelectSet.Select(oNewplane) ' Paste sketch Dim oPasteControlDef As ControlDefinition = oInvApp.CommandManager.ControlDefinitions.Item("AppPasteCmd") oPasteControlDef.Execute()
Maybe not the best way, but a brutal way for execute that. 😉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Cristiano Oliveira
ConsultCAD.com
Hi, I believe that you can modifi the code below to your needs. Or just tell me what else you want it to do and I'll rewrite it for you.
You can specify the file name in highlighted part of code. It works for both assemblies and parts.
You can change the point where the part/assembly will be placed in the second highlighted part of code.
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition Dim Acd As ComponentOccurrencesEnumerator = oAsmCompDef.Occurrences.AllLeafOccurrences Dim Occ As ComponentOccurrence
For Each Occ In Acd ModelFileName = Occ.Definition.Document.FullFileName
FNamePos = InStrRev(ModelFileName, "\", - 1)
docFName = Mid(ModelFileName, FNamePos + 1, Len(ModelFileName) - FNamePos)
If docFName = "MyWantedAssembly.iam" Then Dim oTG As TransientGeometry = ThisApplication.TransientGeometry Dim oMatrix As Matrix = oTG.CreateMatrix Call oMatrix.SetToRotation(3.14159265358979 / 4, _ oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0)) Call oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1)) Dim oOcc As ComponentOccurrence oOcc = oAsmCompDef.Occurrences.Add(ModelFileName, oMatrix)
Exit Sub
End If
Next
Hello Mike,
thanks for the solution. But is it possible to copy 2 or more parts with the constraints and paste them at a given position?
Thank you
Georg
Hi, it is possible, but first I need to know if you want to copy these parts by name (as in my code above) or from selection.
I can also place there an input box and you would write the wanted part/assembly names (with or without extensions).
@Crstiano wrote:Maybe not the best way, but a brutal way for execute that. 😉
Could you please show me a better way to do this? I'm always looking to improve, and be less brutal 😄
I say this because after running the "paste", I need to identify one of the inserted parts, and this way I still do not know how to do!
I do not want to have to keep the whole list of pre-existing items and then find the newly inserted.
So I think the best way is through a collection, but I'm standing (blind) in the way of inserting the collection (with constraints) again and controls it.
I accept suggestions.
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Cristiano Oliveira
ConsultCAD.com
Executing the ControlDefinition for copying is the simplest solution.
You just have to make sure that the Model view window is active otherwise nothing will happen.
The other way would be much more cumbersome as you would have to copy over each component occurrence and then check the constraints between them and then recreate them in the new document.
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Cristiano Oliveira
ConsultCAD.com
Well, I still think you can, someone once wise once said: "If you can do it by hand, you can do it by code" But sometimes it just needs a big workaround, to do something small.
So the question still remains:
Can you copy 2 parts within a assembly that are constraint to each other, and paste them keeping the constraints?
Can it be done by hand? Yes, since 2014?
Select items + copy and paste --> simple as that..
My code may be robust, but it does just that, but it raises another problem, find the latest placed. Could be me, but wouldn't these be the last 2 components in the collection? If you check the count and lets say its 10.. wouldn't 9+10 be your match? (just a idea, got no clue if it is that simple)
Hi,
Yes I think it should be that simple that the last components are the newly created ones:
http://adndevblog.typepad.com/manufacturing/2015/11/copy-occurrences-with-constraints.html
Cheers,