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!

Basic CATIA VBA question

Status
Not open for further replies.

Andzejus

Industrial
Oct 19, 2015
16
LT
Hello,

I'm trying to learn how macros work in CATIA (3DEXPERIENCE R2016x).

I started recording VBA macro (in generative shape design);
Selected xy plane, started a sketch,
drew a circle with radius 3,
exited sketch and used volume extrude with dimension 1.
Then I stopped recording.

Now when I try to play this macro, it gives me an error:
"Run time error '91':
Object variable or With block variable not set"

What is causing this error? I don't think I made something fancy,
and it already throwing errors at me.

Here is the code:
Code:
Sub CATMain()

Dim vPMRepReference1 As VPMRepReference
' No resolution found for the object vPMRepReference1...

Dim part1 As Part
Set part1 = vPMRepReference1.GetItem("Part")

Dim hybridBodies1 As HybridBodies
Set hybridBodies1 = part1.HybridBodies

Dim hybridBody1 As HybridBody
Set hybridBody1 = hybridBodies1.Item("Geometrical Set.4")

Dim sketches1 As Sketches
Set sketches1 = hybridBody1.HybridSketches

Dim sketch1 As Sketch
Set sketch1 = sketches1.Item("Sketch.4")

part1.InWorkObject = sketch1

Dim factory2D1 As Factory2D
Set factory2D1 = sketch1.OpenEdition()

Dim geometricElements1 As GeometricElements
Set geometricElements1 = sketch1.GeometricElements

Dim axis2D1 As Axis2D
Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Dim line2D1 As Line2D
Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Dim line2D2 As Line2D
Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Dim circle2D1 As Circle2D
Set circle2D1 = factory2D1.CreateClosedCircle(0#, 0#, 3#)

Dim point2D1 As Point2D
Set point2D1 = axis2D1.GetItem("Origin")

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 3

Dim point2D2 As Point2D
Set point2D2 = factory2D1.CreatePoint(3#, 0#)

point2D2.ReportName = 4

Dim constraints1 As Constraints
Set constraints1 = sketch1.Constraints

Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromObject(circle2D1)

Dim reference2 As Reference
Set reference2 = part1.CreateReferenceFromObject(point2D2)

Dim constraint1 As Constraint
Set constraint1 = constraints1.AddBiEltCst(catCstTypeOn, reference1, reference2)

constraint1.Mode = catCstModeDrivingDimension

Dim reference3 As Reference
Set reference3 = part1.CreateReferenceFromObject(point2D2)

Dim reference4 As Reference
Set reference4 = part1.CreateReferenceFromObject(line2D1)

Dim constraint2 As Constraint
Set constraint2 = constraints1.AddBiEltCst(catCstTypeOn, reference3, reference4)

constraint2.Mode = catCstModeDrivingDimension

Dim reference5 As Reference
Set reference5 = part1.CreateReferenceFromObject(circle2D1)

Dim constraint3 As Constraint
Set constraint3 = constraints1.AddMonoEltCst(catCstTypeRadius, reference5)

constraint3.Mode = catCstModeDrivingDimension

Dim length1 As Length
Set length1 = constraint3.Dimension

length1.Value = 3#

sketch1.CloseEdition

part1.InWorkObject = hybridBody1

Dim settingControllers1 As SettingControllers
Set settingControllers1 = CATIA.SettingControllers

Dim settingRepository1 As SettingRepository
Set settingRepository1 = settingControllers1.Item("LPCommonEditor")

boolean1 = settingRepository1.GetAttr("LowLightMode")

part1.Update

Dim hybridShapeFactory1 As HybridShapeFactory
Set hybridShapeFactory1 = part1.HybridShapeFactory

Dim hybridShapeDirection1 As HybridShapeDirection
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(0#, 0#, 0#)

Dim reference6 As Reference
Set reference6 = part1.CreateReferenceFromObject(sketch1)

Dim hybridShapeExtrude1 As HybridShapeExtrude
Set hybridShapeExtrude1 = hybridShapeFactory1.AddNewExtrude(reference6, 1#, 0#, hybridShapeDirection1)

hybridShapeExtrude1.Context = 1

hybridShapeExtrude1.SymmetricalExtension = 0

hybridBody1.AppendHybridShape hybridShapeExtrude1

part1.InWorkObject = hybridShapeExtrude1

part1.Update

End Sub

Fifth line is the one that generates error:
Set part1 = vPMRepReference1.GetItem("Part")


 
Replies continue below

Recommended for you

Hi,

This is what I have in 3DEx 2017x cloud. Should work also in 3DEx 2016.

L'm starting from here

Capture_ftnnvo.jpg




Code:
Sub CATMain()

Dim editor1 As Editor
Set editor1 = CATIA.ActiveEditor

Dim part1 As part
Set part1 = editor1.ActiveObject

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 axisSystems1 As AxisSystems
Set axisSystems1 = part1.AxisSystems

Dim axisSystem1 As axisSystem
Set axisSystem1 = axisSystems1.Item("Absolute Axis System")

Dim reference1 As Reference
Set reference1 = part1.CreateReferenceFromBRepName("FSur:(Face:(Brp:(AxisSystem.1;2);None:();Cf11:());WithPermanentBody;WithoutBuildError;WithInitialFeatureSupport;MonoFond;MFBRepVersion_CXR15)", axisSystem1)

Dim sketch1 As Sketch
Set sketch1 = sketches1.Add(reference1)

Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0#
arrayOfVariantOfDouble1(1) = 0#
arrayOfVariantOfDouble1(2) = 0#
arrayOfVariantOfDouble1(3) = 0#
arrayOfVariantOfDouble1(4) = 1#
arrayOfVariantOfDouble1(5) = 0#
arrayOfVariantOfDouble1(6) = 0#
arrayOfVariantOfDouble1(7) = 0#
arrayOfVariantOfDouble1(8) = 1#
Set sketch1Variant = sketch1
sketch1Variant.SetAbsoluteAxisData arrayOfVariantOfDouble1

part1.InWorkObject = sketch1

Dim factory2D1 As Factory2D
Set factory2D1 = sketch1.OpenEdition()

Dim geometricElements1 As GeometricElements
Set geometricElements1 = sketch1.GeometricElements

Dim axis2D1 As Axis2D
Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

Dim line2D1 As Line2D
Set line2D1 = axis2D1.GetItem("HDirection")

line2D1.ReportName = 1

Dim line2D2 As Line2D
Set line2D2 = axis2D1.GetItem("VDirection")

line2D2.ReportName = 2

Dim circle2D1 As Circle2D
Set circle2D1 = factory2D1.CreateClosedCircle(0#, 0#, 84.852814)

Dim point2D1 As Point2D
Set point2D1 = axis2D1.GetItem("Origin")

circle2D1.CenterPoint = point2D1

circle2D1.ReportName = 3

Dim constraints1 As Constraints
Set constraints1 = sketch1.Constraints

Dim reference2 As Reference
Set reference2 = part1.CreateReferenceFromObject(circle2D1)

Dim constraint1 As Constraint
Set constraint1 = constraints1.AddMonoEltCst(catCstTypeRadius, reference2)

constraint1.Mode = catCstModeDrivingDimension

Dim length1 As Length
Set length1 = constraint1.Dimension

length1.Value = 84.852814

sketch1.CloseEdition

part1.InWorkObject = body1

Dim settingControllers1 As SettingControllers
Set settingControllers1 = CATIA.SettingControllers

Dim settingRepository1 As SettingRepository
Set settingRepository1 = settingControllers1.Item("LPCommonEditor")

boolean1 = settingRepository1.GetAttr("LowLightMode")

part1.Update

boolean2 = settingRepository1.GetAttr("DisplayRequirementsMode")

part1.InWorkObject = body1

Dim shapeFactory1 As ShapeFactory
Set shapeFactory1 = part1.ShapeFactory

Dim pad1 As Pad
Set pad1 = shapeFactory1.AddNewPad(sketch1, 20#)

part1.Update

End Sub

Regards
Fernando

- Romania
- EU
 
Hello ferdo,

Thank you for your reply.
I've tested your code on my 3DEx 2016- in generative shape design.
And in my colleagues's 3DEx 2016- in part design.
In both cases it gives the same errors.

Maybe it is because you used catvba?
I don't have catvba option for some reason,
so I used your code with VBA and with CATscript,
and it gives error on different line.

We will be upgrading to 3DEx 2017 soon,
Perhaps I'll just need to wait to find out if different versions is the reason why it doesn't work.
 
I thought you used catvba (vba is written in your code header). VBA from Excel? Or?

Of course the code will look different in CATScript. Can you give more details where are the errors?

Ask your IT to install vba for CATIA, I saw many companies which are not using just because they consider is an security issue...but why then let Office VBA?

Regards
Fernando

- Romania
- EU
 
When I create macro library I have 2 options:
-PLM Directories;
-PLM VBA Directories

If I choose PLM VBA, then macro langiage is "MS VBA",
If I choose PLM Directories, then macro language is "CATScript"

MS-VBA_yyldja.png


CATSCRIPT_f61y7r.png


I chose MS VBA because I thought I could try to transfer data from CATIA to EXCEL later.
 
1 In generative shape design I go to Tools > Macros.
2 I create new macro with MS VBA language. Then I hit 'Edit' and paste your code in a window that appears (the same window is shown on image 4).
3 Then I run this macro and get this error message.
4 When I press 'Debug' I can see a line where error is generated.

order_pgvcdi.png
 
Look at the picture please. When you start to run the macro you need to have already created an axis system, this is how I setup my starting 3D Shape. In 2017x when you want to create a 3D Shape it will be inserted directly under a physical product, that's a difference between 2016 and 2017 if I remember correctly.

Capture_wsodrm.jpg


Regards
Fernando

- Romania
- EU
 
Thank you for showing my error.

Now macro works, but only partially:
It creates a sketch and then stops on this line:

boolean2 = settingRepository1.GetAttr("DisplayRequirementsMode")
 
Yes, now that sketch was extruded and macro finished without errors.
thank you for your patience.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top