Constraints CATIA macro
In the video above you can see a lot of different CATIA macros. All of these macros are based on good ground models and templates. Therefore if you want to have good optimization you must have good templates and basic models. Hence in this post, we want to recreate macro from 0:50-1:00 min of the video. So what this constraint CATIA macro does? It creates constraints for all points inside sketch and round them with the chosen number. I do this operation all the time but manually. Grid for screws is very important, consequently, you can have a problem in production. Certainly with this macro you can easily maintain that problem. With a little bit different code from this you can also check your constraints if they are divided with 10, 5, etc. This is Visual Basic application but we can make it also with CATScript.
Recording macro
Best way to start making these constraints CATIA macro is to record one. You need to make two constraints for the point inside the sketch.
'This is recorded macro, we' ll try to explain all important lines Language="VBSCRIPT" Sub CATMain() 'Next few liens of code are just simple declaration Dim partDocument1 As Document Set partDocument1 = CATIA.ActiveDocument Dim part1 As Part Set part1 = partDocument1.Part Dim bodies1 As Bodies Set bodies1 = part1.Bodies Dim body1 As Body Set body1 = bodies1.Item("PartBody") 'we must change this name later Dim sketches1 As Sketches Set sketches1 = body1.Sketches Dim sketch1 As Sketch Set sketch1 = sketches1.Item("Sketch.2") 'we must change this name later part1.InWorkObject = sketch1 Dim factory2D1 As Factory2D Set factory2D1 = sketch1.OpenEdition() ' allows to enter in sketch 'Simple declarations inside sketch Dim constraints1 As Constraints Set constraints1 = sketch1.Constraints Dim geometricElements1 As GeometricElements Set geometricElements1 = sketch1.GeometricElements 'Declaration of 2D axis inside sketch, and later H and V direction. They are needed for constraints as well. Dim axis2D1 As GeometricElement Set axis2D1 = geometricElements1.Item("AbsoluteAxis") Dim line2D1 As CATBaseDispatch Set line2D1 = axis2D1.GetItem("HDirection") Dim reference1 As Reference Set reference1 = part1.CreateReferenceFromObject(line2D1) Dim point2D1 As GeometricElement Set point2D1 = geometricElements1.Item("Point.2")'main point, we must use some sort of selection Dim reference2 As Reference Set reference2 = part1.CreateReferenceFromObject(point2D1) 'And now with that H direction line and point we can create constrint. Dim constraint1 As Constraint Set constraint1 = constraints1.AddBiEltCst(catCstTypeDistance, reference1, reference2) constraint1.Mode = catCstModeDrivingDimension 'Here we want to set value for that constraint. Dim length1 As Dimension Set length1 = constraint1.Dimension length1.Value = 29.607349 'This code is the same just for H direction Dim line2D2 As CATBaseDispatch Set line2D2 = axis2D1.GetItem("VDirection") Dim reference3 As Reference Set reference3 = part1.CreateReferenceFromObject(line2D2) Dim reference4 As Reference Set reference4 = part1.CreateReferenceFromObject(point2D1) Dim constraint2 As Constraint Set constraint2 = constraints1.AddBiEltCst(catCstTypeDistance, reference3, reference4) constraint2.Mode = catCstModeDrivingDimension Dim length2 As Dimension Set length2 = constraint2.Dimension length2.Value = 15.775104 sketch1.CloseEdition part1.InWorkObject = body1 part1.Update length1.Value = 29.607349 length2.Value = 15.775104 End Sub
Certainly, this recorded macro can help a lot. We just need to change that names, insert some sort of selection and for a loop.
Editing recorded macro
Language="VBSCRIPT" Sub CATMain() 'We well need one inputbox to ask user for that rounding Dim Num as Integer Num=InputBox("Enter number for round, like 1, 2, 5 or 10. This is also your raster") Dim partDocument1 As Document Set partDocument1 = CATIA.ActiveDocument Dim part1 As Part Set part1 = partDocument1.Part 'We need one selection to select all points from active sketch. Dim oSel as Selection Set oSel = CATIA.ActiveDocument.Selection oSel.Search("Name=Point*, in")'this "in" means that it'll search only inside this sketch Dim bodies1 As Bodies Set bodies1 = part1.Bodies Dim body1 As Body 'We defined body1 as parent of point from sketch Set body1 = bodies1.Item(oSel.item(1).value.parent.parent.parent.parent.name) Dim sketches1 As Sketches Set sketches1 = body1.Sketches Dim sketch1 As Sketch 'We defined sketch also as parent of point Set sketch1 = sketches1.Item(oSel.item(1).value.parent.parent.name) part1.InWorkObject = sketch1 Dim constraints1 As Constraints Set constraints1 = sketch1.Constraints Dim geometricElements1 As GeometricElements Set geometricElements1 = sketch1.GeometricElements Dim axis2D1 As GeometricElement Set axis2D1 = geometricElements1.Item("AbsoluteAxis") Dim line2D1 As CATBaseDispatch Set line2D1 = axis2D1.GetItem("HDirection") Dim line2D2 As CATBaseDispatch Set line2D2 = axis2D1.GetItem("VDirection") Dim reference1 As Reference Set reference1 = part1.CreateReferenceFromObject(line2D1) Dim reference3 As Reference Set reference3 = part1.CreateReferenceFromObject(line2D2) 'We used for loop to loop through all selected points from sketch For i=1 to oSel.count Dim point2D1 As GeometricElement 'We defined point also from selection Set point2D1 = geometricElements1.Item(osel.item(i).value.name) Dim reference2 As Reference Set reference2 = part1.CreateReferenceFromObject(point2D1) Dim constraint1 As Constraint Set constraint1 = constraints1.AddBiEltCst(catCstTypeDistance, reference1, reference2) constraint1.Mode = catCstModeDrivingDimension Dim length1 As Dimension Set length1 = constraint1.Dimension 'we use here that input box and round method. You can read here more about rounding https://stackoverflow.com/questions/326476/rounding-a-number-to-the-nearest-5-or-10-or-x length1.value=round(length1.value/Num)*Num Dim reference4 As Reference Set reference4 = part1.CreateReferenceFromObject(point2D1) Dim constraint2 As Constraint Set constraint2 = constraints1.AddBiEltCst(catCstTypeDistance, reference3, reference4) constraint2.Mode = catCstModeDrivingDimension Dim length2 As Dimension Set length2 = constraint2.Dimension length2.value=round(length2.value/Num)*Num next part1.Update End Sub
This is our final code hence just copy and paste it. So if you want to use this macro make a sketch and few points inside. From the sketch environment run these constraints CATIA macro like in the video below.
You can copy-paste this macro code and use it.
If you have any issues feel free to contact me.
Read even more about selection on these two posts: https://catiavbmacro.com/2018/08/26/catia-macro-selection/
http://www.scripting4v5.com/additional-articles/catia-macro-selection/
This sounds kinda complicated…imo…
Hi, well i can understand it, but it can be very helpful once you got it.
If you have any questions feel free to ask.
im getting microsoft vb script compilation error
description :expected end of statement
satement:dim num as integer
line:4
column:8
Hello, there is no problem with this line Dim num As Integer. Maybe sowhere else in the code. Can you send me the full code to check it.
Hi, I am trying to automate constraints in the product document (i.e., assembly). I use the following code to import the parts:
products2.AddComponentsFromFiles arrayofvariantofbstr1,”All”
If I want to create a reference1 using a geometrical point inside the inserted part, how can I do that using macro? The inserted part has only one geometrical point in it. Thanks!
Hello, I didnt make assy constraints like that. I always use one coincidence constraint. For that you will need axis system in both parts, and connect those two. In this case it will be much easier to create assy constraint. I hope this help.