Copy Part CATIA macro
Introduction
First of all, almost in every model, there are more instances of the same Part. So how do you make those instances and constraints for them? Are you doing it manually? Well if you do, certainly there is a better and faster way for that. Hence let’s write some code for that action.
Language="VBSCRIPT" Sub CATMain() Dim productDocument1 As Document Set productDocument1 = CATIA.ActiveDocument 'We must define selection because we need to select that part for copy Dim oSel Set oSel = productDocument1.Selection Dim strArray(0) strArray(0) = "Product" Dim sStatus As String sStatus = oSel.SelectElement3(strArray, "Select main part", False, CATMultiSelectionMode.CATMultiSelTriggWhenUserValidatesSelection, False) Dim CopyPart Set CopyPart = oSel.item(1).value 'Defined part using selection Dim copyproduct Set copyproduct = CopyPart.parent.parent 'We need to define product where is that part, also using selection 'Add that part to selection oSel.Add(CopyPart) oSel.Copy() oSel.Clear() 'Add product to selection oSel.Add(copyproduct) oSel.Paste() End Sub
Additional
That was just a simple code of copy Part CATIA macro for one instance of the selected part. You can also use for loop to make more instances, just one or two lines of code.
If the selected part has constraints you can copy those constraints or not.
Furthermore here in the options tab, you can change these options. For example, now it’s enabled copy instances without the assembly constraints. So if you run previous copy Part CATIA macro, and your main part has constraints, they will not be copied.
You can change these options with macro too.
Dim settingControllers1 As SettingControllers settingControllers1 = MyCATIA.SettingControllers Dimm asmConstraintSettingAtt1 As SettingController asmConstraintSettingAtt1 = settingControllers1.Item("CATAsmConstraintSettingCtrl") Dim long1 As Long long1 = asmConstraintSettingAtt1.RedundancyMode Dim catPasteWithoutCsts As Object = Nothing asmConstraintSettingAtt1.PasteComponentMode = catPasteWithoutCsts asmConstraintSettingAtt1.SaveRepository()
So this code is to enable copy without constraints. We need this when we want to create constraints with macro. If they are also copied we have two constraints for the same instance. All of these options you can record with a macro.
Also, you can add constraints to those instances.
Dim constraints1 As Collection Set constraints1 = copyproduct.Connections("CATIAConstraints") Dim reference1 As Reference Set reference1 = copyproduct.CreateReferenceFromName( copyproduct.referenceproduct.name & "/" & osel.item(1).value.name & "/!" & copyproduct.referenceproduct.name & "/" & osel.item(1).value.name &"/")
We make Fix constraint. For example, you can make any type of constraint. But if you don’t know how you can always record it. All inputs and declarations in this code are made from that first selection.
CopyPart = oSel.item(1).value 'Part for copy copyproduct = CopyPart.parent.parent 'Product of the part constraints1 = copyproduct.Connections("CATIAConstraints") 'Constraints are connected with copyproduct copyproduct.referenceproduct.name 'Referenceproduct name osel.item(1).value.name 'Instance name
Therefore users just need to select that first part and we will define it all based on it. So you can just add this second code to the first one and get a full macro.
Finally, if you need to copy part with brake link, you can do it with this code:
oSel.PasteSpecial("CATSpecBreakLink")