Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

CATIA Macro for Assembly Constraints

Status
Not open for further replies.

ebdulkareem

Mechanical
Nov 27, 2023
1
I have the following CATIA Script that does the following job in the product document:

1. Asks the user to select target points (which are geometrical points and not from a sketch) where they want bolts to be inserted,
2. Retrieves the location of the part file of the bolt from the user,
3. Creates a level 1 product in the tree (i.e., a subassembly),
4. 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 a geometrical point (which is different from sketch points) in it. I want the macro to add a coincidence constraint to each point in the parent part (that was selected by the user in step 1) with a point in each inserted part. How can I achieve this? Also, I will be working with many other bolts but each of them contains a geometrical point (like described above), so I want it to be generalised. I have very, very little knowledge about scripting macros, and I got the following script from the internet. I would appreciate it if anyone could help.

Code:
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 
Dim strFilePath As String
 
'DISPLAY FILE OPEN DIALOG BOX
strFilePath = CATIA.FileSelectionBox("Select Part File", "*.CATPart", 0)
 
'IF USER CLICKED CANCEL (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 the Product document:
dim product1 as product
set product1 = partdocument1.product

Dim products1 as products
Set products1=product1.products

'CREATING 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
 
Replies continue below

Recommended for you

Hi,

If the links in the previous replies will help you getting the VBA objets and methods to do it, you also need to make your sub-assembly "not rigid". If it is rigid, then you can't constraint more than one bolt to parts outside of that sub-assembly. (You can, but it won't update in CATIA if they are all active).

regards
Thierry

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor