I told you how to add a sketch and only a sketch. Now I present you some examples and exercices to modify sketches.
WARNING : If you want to use the macro recorder, don't forget to leave the sketch mode by the exit workbench button, or nothing will be recorded.
First, here you have the basic lines, which are needed every time, with the two alternatives :
- With the sketch directly in the main body
sub your_sub() Dim CATIA As Object On Error Resume Next Set CATIA = GetObject("CATIA.Application") If Err.Number <> 0 Then Set CATIA = CreateObject("CATIA.Application") CATIA.Visible=True End if On Error GoTo 0 Dim myDocument As Documents Set myDocument = CATIA.documents Dim partDocument1 As partDocument Set partDocument1 = myDocument.Add("Part") 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") Dim sketches1 As Sketches Set sketches1 = body1.Sketches Dim originelements1 As OriginElements Set originelements1 = part1.OriginElements Dim reference1 As Reference Set reference1 = originelements1.PlaneXY Dim sketch1 As Sketch Set sketch1 = sketches1.Add(reference1) Dim factory2D1 as Factory2D Set factory2D1 = sketch1.OpenEdition sketch1.CloseEdition part1.Update End sub |
- With the sketch in your geometrical set
sub your_sub() Dim CATIA As Object On Error Resume Next Set CATIA = GetObject("CATIA.Application") If Err.Number <> 0 Then Set CATIA = CreateObject("CATIA.Application") CATIA.Visible=True End if On Error GoTo 0 Dim myDocument As Documents Set myDocument = CATIA.documents Dim partDocument1 As partDocument Set partDocument1 = myDocument.Add("Part") Dim part1 As part Set part1=partDocument1.Part Dim hybridBodies1 As HybridBodies Set hybridBodies1 = part1.HybridBodies Dim hybridBody1 As HybridBody Set hybridBody1 = hybridBodies1.Item("Geometrical Set.1") Dim sketches1 As Sketches Set sketches1 = hybridbody1.HybridSketches Dim originelements1 As OriginElements Set originelements1 = part1.OriginElements Dim reference1 As Reference Set reference1 = originelements1.PlaneXY Dim sketch1 As Sketch Set sketch1 = sketches1.Add(reference1) Dim factory2D1 as Factory2D set factory2D1 = sketch1.OpenEdition sketch1.CloseEdition part1.Update End sub |
You can just copy/paste this code.
Now it's possible to modify this sketch. I suppose that the sketch is in your geometrical set (the code with the sketch directly in the body is slightly the same)
First I make a short list of the useful items in the sketch mode :
Refer to the help for the definition of more methods
CreateCircle
Creates and returns a 2D circle arc.
CreateClosedCircle
Creates and returns a closed 2D circle.
CreateClosedEllipse
Creates and returns a closed 2D ellipse.
CreateControlPoint
Creates and returns a 2D spline control point.
CreateEllipse
Creates and returns a 2D ellipse arc.
CreateHyperbola
Creates and returns a hyperbola.
CreateIntersection
Creates and returns the intersection of an object with the sketch.
CreateIntersections
Creates and returns the possible intersections of an object with the sketch.
CreateLine
Creates and returns a 2D line.
CreateLineFromVector
Creates and returns a 2D line.
CreateParabola
Creates and returns a parabola.
CreatePoint
Creates and returns a 2D point.
CreateProjection
Creates and returns the projection of an object on the sketch.
CreateProjections
Creates and returns the possible projections of an object on the sketch.
CreateSpline
Creates and returns a 2D b-spline.
Don't forget to use the macro recorder when you need a specific thing, it's a rough code but always useful.
Let's try a little crossover exercice between CATIA and Excel
Exercice :
You have an Excel table available which contain coordinates of points. Create a spline in CATIA V5 from this table.
Then create a circle : center (200,90) , radius = 22
Here for the excel file
Let's head to the Part Design workbench !
Now it's possible to modify this sketch. I suppose that the sketch is in your geometrical set (the code with the sketch directly in the body is slightly the same)
First I make a short list of the useful items in the sketch mode :
'To create a point Dim point2D1 As Point2D Set point2D1 = factory2D1.CreatePoint(your_H_coordinate, your_V_coordinate) 'To create a line 'Create the line using this command line Dim line2D1 As Line2D Set line2D1 = factory2D1.CreateLine(first_point_H_coordinate, first_point_V_coordinate, second_point_H_coordinate, second_point_V_coordinate) 'To create a circle Dim circle2D1 As Circle2D Set circle2D1 = factory2D1.CreateClosedCircle(center_H_coordinate, center_V_coordinate, radius_of_your_circle) 'To create a spline 'A spline needs control points instead of simple points 'Create a spline with three control points like this : Dim controlPoint2D1 As ControlPoint2D Set controlPoint2D1 = factory2D1.CreateControlPoint(H1, V1) Dim controlPoint2D2 As ControlPoint2D Set controlPoint2D2 = factory2D1.CreateControlPoint(H2, V2) Dim controlPoint2D3 As ControlPoint2D Set controlPoint2D3 = factory2D1.CreateControlPoint(H3, V3) 'The CreateSpline function needs a CATSafeArrayVariant according to the help 'Create an array of object like this Dim arrayOfObject1(2) Set arrayOfObject1(0) = controlPoint2D1 Set arrayOfObject1(1) = controlPoint2D2 Set arrayOfObject1(2) = controlPoint2D3 Dim spline2D1 As Spline2D Set factory2D1temp=factory2D1 Set spline2D1 = factory2D1temp.CreateSpline(arrayOfObject1) |
Refer to the help for the definition of more methods
CreateCircle
Creates and returns a 2D circle arc.
CreateClosedCircle
Creates and returns a closed 2D circle.
CreateClosedEllipse
Creates and returns a closed 2D ellipse.
CreateControlPoint
Creates and returns a 2D spline control point.
CreateEllipse
Creates and returns a 2D ellipse arc.
CreateHyperbola
Creates and returns a hyperbola.
CreateIntersection
Creates and returns the intersection of an object with the sketch.
CreateIntersections
Creates and returns the possible intersections of an object with the sketch.
CreateLine
Creates and returns a 2D line.
CreateLineFromVector
Creates and returns a 2D line.
CreateParabola
Creates and returns a parabola.
CreatePoint
Creates and returns a 2D point.
CreateProjection
Creates and returns the projection of an object on the sketch.
CreateProjections
Creates and returns the possible projections of an object on the sketch.
CreateSpline
Creates and returns a 2D b-spline.
Don't forget to use the macro recorder when you need a specific thing, it's a rough code but always useful.
Let's try a little crossover exercice between CATIA and Excel
Exercice :
You have an Excel table available which contain coordinates of points. Create a spline in CATIA V5 from this table.
Then create a circle : center (200,90) , radius = 22
Here for the excel file
Spoiler:
Let's head to the Part Design workbench !
Hello, I get and error from the Excel Compiler that highlights ".CreateSpline" and says "Function or Interface marked as restricted, or the function uses an Automation type not supported in Visual Basic." After some time investigating I am sure the problem is because VBA does not recognize CATSafeVariantArray. How did you get around this?
RépondreSupprimer