Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

CATIA MACRO with INPUT SELECTION OPTION to user

Status
Not open for further replies.

DomKum

Automotive
Nov 27, 2005
23
IN
Hi

Below is the macro with input AS POINT and reference PLANE as selected by user, it will create a SKETCH inside which there is predefined CIRCLE.
I will create another SKETCH which is having a PREDEFINED SQUARE in side.

My requirement is when this MACRO is run,
I want MACRO ask user for below two options and based on the selection, sketch has to be created.
1. Sketch with Circle
2. Sketch with SQUARE.

How to go forward this using input options.
Please share your thoughts. Thanks in advance

PS: Please share link for MACROS in which INPUT BOX is used for allowing the user to selection options available in macro.

Language="VBSCRIPT"

Sub CATMain()

Set partDocument1 = CATIA.ActiveDocument

Set part1 = partDocument1.Part

Set hybridBodies1 = part1.HybridBodies

'----->with below line the Sketch will always be created in GS1.
'Set hybridBody1 = hybridBodies1.Item("Open_body.1")

'---->>> with below line sketch will always be created in acitve GS
Set hybridBody1 = CATIA.ActiveDocument.Part.InWorkObject

Msgbox("Pickup the reference plane for sketch")
Dim InputObjectType(0), Status1
Set selection1 = partDocument1.Selection
selection1.Clear
InputObjectType(0)="Plane"
Status1=selection1.SelectElement2(InputObjectType,"Select plane",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference1 = selection1.Item(1).Reference
MsgBox reference1.Name

Msgbox("Pickup the reference point to sketch reference")
InputObjectType(0)="Point"
Status1=selection1.SelectElement2(InputObjectType,"Reference point",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference2 = selection1.Item(1).Reference
MsgBox reference2.Name


Set sketches1 = hybridBody1.HybridSketches

'-----Created by default macro recording---
'Set sketch1 = sketches1.Item("Sketch.1")

'Set sketch1 = body1.Sketches

'------------------------------

Set originElements1 = part1.OriginElements

Set sketch1 = sketches1.Add(reference1)


'Set sketch1 = originElements1.reference1


part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(10.000000, 10.000000)

point2D1.ReportName = 3

Set circle2D1 = factory2D1.CreateClosedCircle(10.000000, 10.000000, 4.000000)

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 4

Set constraints1 = sketch1.Constraints

Set reference1 = part1.CreateReferenceFromObject(circle2D1)

Set constraint1 = constraints1.AddMonoEltCst(catCstTypeRadius, reference1)

constraint1.Mode = catCstModeDrivingDimension

Set length1 = constraint1.Dimension

length1.Value = 4.000000

Set reference2 = part1.CreateReferenceFromObject(line2D1)

Set reference3 = part1.CreateReferenceFromObject(point2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeDistance, reference2, reference3)

constraint2.Mode = catCstModeDrivingDimension

Set length2 = constraint2.Dimension

length2.Value = 10.000000

Set reference4 = part1.CreateReferenceFromObject(line2D2)

Set reference5 = part1.CreateReferenceFromObject(point2D1)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeDistance, reference4, reference5)

constraint3.Mode = catCstModeDrivingDimension

Set length3 = constraint3.Dimension

length3.Value = 10.000000

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

part1.UpdateObject sketch1

part1.Update

End Sub
 
Replies continue below

Recommended for you

"input box" - google - pick any link
Done in 3 seconds.

Respect forum members, save their time.
Questions about general programming is not what we here for.
 
Hi All,

Finally I could able to make MACRO with selection 1.Circle or 2.Rectangle.

I would be more happy for me if this will be useful to any new beginners like me.

My next step is to create PAD from it. Hope it works.


Language="VBSCRIPT"

Sub CATMain()

Set partDocument1 = CATIA.ActiveDocument

Set part1 = partDocument1.Part

Set hybridBodies1 = part1.HybridBodies

'----->with below line the Sketch will always be created in GS1.
'Set hybridBody1 = hybridBodies1.Item("Open_body.1")

'---->>> with below line sketch will always be created in acitve GS
Set hybridBody1 = CATIA.ActiveDocument.Part.InWorkObject

MsgBox ("This Macro is to create sketch for Circle or Rectangle")

Msgbox("Pickup the reference plane for sketch")
Dim InputObjectType(0), Status1
Set selection1 = partDocument1.Selection
selection1.Clear
InputObjectType(0)="Plane"
Status1=selection1.SelectElement2(InputObjectType,"Select plane",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference1 = selection1.Item(1).Reference
MsgBox reference1.Name

Msgbox("Pickup the reference point to sketch reference")
InputObjectType(0)="Point"
Status1=selection1.SelectElement2(InputObjectType,"Reference point",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference2 = selection1.Item(1).Reference
MsgBox reference2.Name

'Dim Choice ' ' As Integer
Choice = InputBox("Please Select 1 for Circle or 2 for Rectangle")

' - - - - - > > > > > ----- Circle Strart - -- - - - - - - - - - - - - -

If Choice = 1 Then

Set sketches1 = hybridBody1.HybridSketches
Set originElements1 = part1.OriginElements

Set sketch1 = sketches1.Add(reference1)

'Set sketch1 = originElements1.reference1

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(10.000000, 10.000000)

point2D1.ReportName = 3

Set circle2D1 = factory2D1.CreateClosedCircle(10.000000, 10.000000, 4.000000)

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 4

Set constraints1 = sketch1.Constraints

Set reference1 = part1.CreateReferenceFromObject(circle2D1)

Set constraint1 = constraints1.AddMonoEltCst(catCstTypeRadius, reference1)

constraint1.Mode = catCstModeDrivingDimension

Set length1 = constraint1.Dimension

length1.Value = 4.000000

Set reference2 = part1.CreateReferenceFromObject(line2D1)

Set reference3 = part1.CreateReferenceFromObject(point2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeDistance, reference2, reference3)

constraint2.Mode = catCstModeDrivingDimension

Set length2 = constraint2.Dimension

length2.Value = 10.000000

Set reference4 = part1.CreateReferenceFromObject(line2D2)

Set reference5 = part1.CreateReferenceFromObject(point2D1)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeDistance, reference4, reference5)

constraint3.Mode = catCstModeDrivingDimension

Set length3 = constraint3.Dimension

length3.Value = 10.000000

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

part1.UpdateObject sketch1

' - - - - - > > > > > ----- Circle End - -- - - - - - - - - - - - - -

Else Choice = 2
'MsgBox " You selecion is " & 2


' - - - - - > > > > > ----- Rectangle Start - -- - - - - - - - - - - - - -
'Set sketch1 = sketches1.Item("Sketch.5")
'part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(15.000000, 40.000000)

point2D1.ReportName = 3

Set point2D2 = factory2D1.CreatePoint(65.000000, 40.000000)

point2D2.ReportName = 4

Set line2D3 = factory2D1.CreateLine(15.000000, 40.000000, 65.000000, 40.000000)

line2D3.ReportName = 5

line2D3.StartPoint = point2D1

line2D3.EndPoint = point2D2

Set point2D3 = factory2D1.CreatePoint(65.000000, 15.000000)

point2D3.ReportName = 6

Set line2D4 = factory2D1.CreateLine(65.000000, 40.000000, 65.000000, 15.000000)

line2D4.ReportName = 7

line2D4.EndPoint = point2D2

line2D4.StartPoint = point2D3

Set point2D4 = factory2D1.CreatePoint(15.000000, 15.000000)

point2D4.ReportName = 8

Set line2D5 = factory2D1.CreateLine(65.000000, 15.000000, 15.000000, 15.000000)

line2D5.ReportName = 9

line2D5.StartPoint = point2D3

line2D5.EndPoint = point2D4

Set line2D6 = factory2D1.CreateLine(15.000000, 15.000000, 15.000000, 40.000000)

line2D6.ReportName = 10

line2D6.EndPoint = point2D4

line2D6.StartPoint = point2D1

Set constraints1 = sketch1.Constraints

Set reference1 = part1.CreateReferenceFromObject(line2D3)

Set reference2 = part1.CreateReferenceFromObject(line2D1)

Set constraint1 = constraints1.AddBiEltCst(catCstTypeHorizontality, reference1, reference2)

constraint1.Mode = catCstModeDrivingDimension

Set reference3 = part1.CreateReferenceFromObject(line2D5)

Set reference4 = part1.CreateReferenceFromObject(line2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeHorizontality, reference3, reference4)

constraint2.Mode = catCstModeDrivingDimension

Set reference5 = part1.CreateReferenceFromObject(line2D4)

Set reference6 = part1.CreateReferenceFromObject(line2D2)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeVerticality, reference5, reference6)

constraint3.Mode = catCstModeDrivingDimension

Set reference7 = part1.CreateReferenceFromObject(line2D6)

Set reference8 = part1.CreateReferenceFromObject(line2D2)

Set constraint4 = constraints1.AddBiEltCst(catCstTypeVerticality, reference7, reference8)

constraint4.Mode = catCstModeDrivingDimension

Set reference9 = part1.CreateReferenceFromObject(line2D1)

Set reference10 = part1.CreateReferenceFromObject(line2D5)

Set constraint5 = constraints1.AddBiEltCst(catCstTypeDistance, reference9, reference10)

constraint5.Mode = catCstModeDrivingDimension

Set length1 = constraint5.Dimension

length1.Value = 15.000000

Set reference11 = part1.CreateReferenceFromObject(line2D2)

Set reference12 = part1.CreateReferenceFromObject(line2D6)

Set constraint6 = constraints1.AddBiEltCst(catCstTypeDistance, reference11, reference12)

constraint6.Mode = catCstModeDrivingDimension

Set length2 = constraint6.Dimension

length2.Value = 15.000000

Set reference13 = part1.CreateReferenceFromObject(line2D4)

Set constraint7 = constraints1.AddMonoEltCst(catCstTypeLength, reference13)

constraint7.Mode = catCstModeDrivingDimension

Set length3 = constraint7.Dimension

length3.Value = 25.000000

Set reference14 = part1.CreateReferenceFromObject(line2D6)

Set reference15 = part1.CreateReferenceFromObject(line2D4)

Set constraint8 = constraints1.AddBiEltCst(catCstTypeDistance, reference14, reference15)

constraint8.Mode = catCstModeDrivingDimension

Set length4 = constraint8.Dimension

length4.Value = 50.000000

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

part1.UpdateObject sketch1


End if
part1.Update

End Sub
 
Hi
This macro is with creation of PAD.
Hope this will be useful for new beginners

Language="VBSCRIPT"

Sub CATMain()

Set partDocument1 = CATIA.ActiveDocument

Set part1 = partDocument1.Part

Set hybridBodies1 = part1.HybridBodies

'----->with below line the Sketch will always be created in GS1.
'Set hybridBody1 = hybridBodies1.Item("Open_body.1")

'---->>> with below line sketch will always be created in acitve GS
Set hybridBody1 = CATIA.ActiveDocument.Part.InWorkObject

MsgBox ("This Macro is to create sketch for Circle or Rectangle")

Msgbox("Pickup the reference plane for sketch")
Dim InputObjectType(0), Status1
Set selection1 = partDocument1.Selection
selection1.Clear
InputObjectType(0)="Plane"
Status1=selection1.SelectElement2(InputObjectType,"Select plane",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference1 = selection1.Item(1).Reference
MsgBox reference1.Name

Msgbox("Pickup the reference point to sketch reference")
InputObjectType(0)="Point"
Status1=selection1.SelectElement2(InputObjectType,"Reference point",false)
If Status1 = "Cancel" Then selection1.Clear: Exit Sub
Set reference2 = selection1.Item(1).Reference
MsgBox reference2.Name

'Dim Choice ' ' As Integer
Choice = InputBox("Please Select 1 for Circle or 2 for Rectangle")

' - - - - - > > > > > ----- Circle Strart - -- - - - - - - - - - - - - -

If Choice = 1 Then

Set sketches1 = hybridBody1.HybridSketches
Set originElements1 = part1.OriginElements

Set sketch1 = sketches1.Add(reference1)

'Set sketch1 = originElements1.reference1

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(10.000000, 10.000000)

point2D1.ReportName = 3

Set circle2D1 = factory2D1.CreateClosedCircle(10.000000, 10.000000, 4.000000)

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 4

Set constraints1 = sketch1.Constraints

Set reference1 = part1.CreateReferenceFromObject(circle2D1)

Set constraint1 = constraints1.AddMonoEltCst(catCstTypeRadius, reference1)

constraint1.Mode = catCstModeDrivingDimension

Set length1 = constraint1.Dimension

length1.Value = 4.000000

Set reference2 = part1.CreateReferenceFromObject(line2D1)

Set reference3 = part1.CreateReferenceFromObject(point2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeDistance, reference2, reference3)

constraint2.Mode = catCstModeDrivingDimension

Set length2 = constraint2.Dimension

length2.Value = 10.000000

Set reference4 = part1.CreateReferenceFromObject(line2D2)

Set reference5 = part1.CreateReferenceFromObject(point2D1)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeDistance, reference4, reference5)

constraint3.Mode = catCstModeDrivingDimension

Set length3 = constraint3.Dimension

length3.Value = 10.000000

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

part1.UpdateObject sketch1

' - - - - - > > > > > ----- Circle End - -- - - - - - - - - - - - - -


' - - - - - > > > > > ----- Rectangle Start - -- - - - - - - - - - - - - -
Else Choice = 2
'MsgBox " You selecion is " & 2

Set sketches1 = hybridBody1.HybridSketches
Set originElements1 = part1.OriginElements

Set sketch1 = sketches1.Add(reference1)

part1.InWorkObject = sketch1

Set factory2D1 = sketch1.OpenEdition()

Set geometricElements1 = sketch1.GeometricElements

Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Set point2D1 = factory2D1.CreatePoint(15.000000, 40.000000)

point2D1.ReportName = 3

Set point2D2 = factory2D1.CreatePoint(65.000000, 40.000000)

point2D2.ReportName = 4

Set line2D3 = factory2D1.CreateLine(15.000000, 40.000000, 65.000000, 40.000000)

line2D3.ReportName = 5

line2D3.StartPoint = point2D1

line2D3.EndPoint = point2D2

Set point2D3 = factory2D1.CreatePoint(65.000000, 15.000000)

point2D3.ReportName = 6

Set line2D4 = factory2D1.CreateLine(65.000000, 40.000000, 65.000000, 15.000000)

line2D4.ReportName = 7

line2D4.EndPoint = point2D2

line2D4.StartPoint = point2D3

Set point2D4 = factory2D1.CreatePoint(15.000000, 15.000000)

point2D4.ReportName = 8

Set line2D5 = factory2D1.CreateLine(65.000000, 15.000000, 15.000000, 15.000000)

line2D5.ReportName = 9

line2D5.StartPoint = point2D3

line2D5.EndPoint = point2D4

Set line2D6 = factory2D1.CreateLine(15.000000, 15.000000, 15.000000, 40.000000)

line2D6.ReportName = 10

line2D6.EndPoint = point2D4

line2D6.StartPoint = point2D1

Set constraints1 = sketch1.Constraints

Set reference1 = part1.CreateReferenceFromObject(line2D3)

Set reference2 = part1.CreateReferenceFromObject(line2D1)

Set constraint1 = constraints1.AddBiEltCst(catCstTypeHorizontality, reference1, reference2)

constraint1.Mode = catCstModeDrivingDimension

Set reference3 = part1.CreateReferenceFromObject(line2D5)

Set reference4 = part1.CreateReferenceFromObject(line2D1)

Set constraint2 = constraints1.AddBiEltCst(catCstTypeHorizontality, reference3, reference4)

constraint2.Mode = catCstModeDrivingDimension

Set reference5 = part1.CreateReferenceFromObject(line2D4)

Set reference6 = part1.CreateReferenceFromObject(line2D2)

Set constraint3 = constraints1.AddBiEltCst(catCstTypeVerticality, reference5, reference6)

constraint3.Mode = catCstModeDrivingDimension

Set reference7 = part1.CreateReferenceFromObject(line2D6)

Set reference8 = part1.CreateReferenceFromObject(line2D2)

Set constraint4 = constraints1.AddBiEltCst(catCstTypeVerticality, reference7, reference8)

constraint4.Mode = catCstModeDrivingDimension

Set reference9 = part1.CreateReferenceFromObject(line2D1)

Set reference10 = part1.CreateReferenceFromObject(line2D5)

Set constraint5 = constraints1.AddBiEltCst(catCstTypeDistance, reference9, reference10)

constraint5.Mode = catCstModeDrivingDimension

Set length1 = constraint5.Dimension

length1.Value = 15.000000

Set reference11 = part1.CreateReferenceFromObject(line2D2)

Set reference12 = part1.CreateReferenceFromObject(line2D6)

Set constraint6 = constraints1.AddBiEltCst(catCstTypeDistance, reference11, reference12)

constraint6.Mode = catCstModeDrivingDimension

Set length2 = constraint6.Dimension

length2.Value = 15.000000

Set reference13 = part1.CreateReferenceFromObject(line2D4)

Set constraint7 = constraints1.AddMonoEltCst(catCstTypeLength, reference13)

constraint7.Mode = catCstModeDrivingDimension

Set length3 = constraint7.Dimension

length3.Value = 25.000000

Set reference14 = part1.CreateReferenceFromObject(line2D6)

Set reference15 = part1.CreateReferenceFromObject(line2D4)

Set constraint8 = constraints1.AddBiEltCst(catCstTypeDistance, reference14, reference15)

constraint8.Mode = catCstModeDrivingDimension

Set length4 = constraint8.Dimension

length4.Value = 50.000000

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

part1.UpdateObject sketch1

part1.Update

End if

' - - - - - > > > > > ----- Rectangle End - -- - - - - - - - - - - - - -

' - - - - - This line was created for testing -- working fine----
'If Choice = 1 Then
'MsgBox ("Your Selection is " & 1 & " - Circle")


'Else

'MsgBox ("Your Selection is " & 2 & " - Rectangle")


'End if
'--------- This is for testing only --------

' - - - - - Creation of PAD from the sketch above

PAD = Msgbox("Do you want to create PAD" , vbYesNo+vbQuestion, "Tell me")

If PAD = vbYes Then

'Msgbox "PAD"


Set bodies1 = part1.Bodies
Set body1 = bodies1.Add()
part1.InWorkObject = body1
Set shapeFactory1 = part1.ShapeFactory
Set reference1 = part1.CreateReferenceFromName("")
Set pad1 = shapeFactory1.AddNewPadFromRef(reference1, 20.000000)
Set hybridBodies1 = part1.HybridBodies
'Set hybridBody1 = hybridBodies1.Item("Geometrical Set.3")
Set sketches1 = hybridBody1.HybridSketches
'Set sketch1 = sketches1.Item("Sketch.22")
Set reference2 = part1.CreateReferenceFromObject(sketch1)
pad1.SetProfileElement reference2
pad1.IsSymmetric = True
part1.UpdateObject pad1

Else

MsgBox " Thank you for using this macro"
End if

part1.Update

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top