I have a CATIA Script that does the following job in the product document:
- Asks the user to select target points (which are geometrical points and not from a sketch) where they want bolts to be inserted,
- Retrieves the location of the part file of the bolt from the user,
- Creates a level 1 product in the tree (i.e., a subassembly),
- Inserts the bolts in the subassembly as many times as there were points selected in step 1.
Now, every part file of the bolt that is inserted has only one geometrical point (which differs from a sketch point) in it. I want the macro to add a coincidence constraint on each selected point in the parent part (i.e., points which were selected by the user in step 1) with the point in each inserted part. How can I achieve this? Is it possible to create references from parts inserted via AddComponentsFromFiles? Also, I will be working with different bolts (meaning the names of files will vary) but each of them contains a geometrical point (like described above), so I want it to be generalised. My design tree may contain other subassemblies or parts so I do not know how can I use something like CreateReferenceFromName for this. I have very, very little knowledge about scripting macros, and I got the following script from the internet but I understand almost all of it. The script I am using is as follows.
Sub CATMain()
'====================SELECT THE ACTIVE DOCUMENT
Dim mysel
set mysel = CATIA.ActiveDocument.selection
'====================CREATE A FILTER SO THAT THE USER CAN SELECT POINTS ONLY
dim myfilter(0)
myfilter(0)="Point"
'====================TELL CATIA TO PROMPT THE USER TO SELECT THE POINTS
dim mypoints
mypoints = mysel.SelectElement3(myfilter,"Select the target points for insertion of parts",true,CATMultiSelTriggWhenUserValidatesSelection,false)
if (status = "Cancel") then exit sub
'====================COUNT THE POINTS
for x=1 to mysel.count
set point=mysel.item(x).value
next
'====================RETRIEVE THE PATH OF THE PART FROM THE USER USING DIALOG BOX
Dim strFilePath As String
strFilePath = CATIA.FileSelectionBox("Select Part File", "*.CATPart", 0)
'====================IF USER CLICKED CANCEL (i.e., empty string is returned), EXIT THE PROGRAM
if strFilePath = "" Then Exit Sub
Dim ofilesys as filesystem
Set ofilesys=CATIA.filesystem
if (not ofilesys.fileexists(strfilepath)) then
'err.raise 9999,,sfilepath & ": This file does not exist."
msgbox "Error: The file does not exist."
else
dim arrayOfVariantOfBSTR1(0)
arrayOfVariantOfBSTR1(0)=strfilepath
dim partdocument1 as document
set partdocument1 = CATIA.ActiveDocument
'====================GET THE ROOT PRODUCT FROM PRODUCT DOCUMENT
dim product1 as product
set product1 = partdocument1.product
Dim products1 as products
Set products1 = product1.products
'====================CREATE A SUBASSEMBLY
Dim Product2 as product
Set Product2 = products1.AddNewComponent("Product", "")
Dim products2 as products
Set products2 = product2.products
'====================INSERTING PARTS AS MANY TIMES
dim instancesss
For instancesss=1 to mysel.count
products2.AddComponentsFromFiles arrayOfVariantOfBSTR1,"All"
Next
Msgbox "Parts succesfully inserted." & vbnewline & "No. of parts inserted: " & mysel.count, 0
end if
End Sub
I have tried creating the reference using reference1=product2.CreateReferenceFromName(___ & "/" & ____ "/!" & ____)
etc., but got lost because I do not know much about scripting.