VBA Example

VBA Example

ravikmb5
Collaborator Collaborator
3,682 Views
10 Replies
Message 1 of 11

VBA Example

ravikmb5
Collaborator
Collaborator

 

 

Control point, equation, and intersection curve creation. API Sample

 

i am getting  Error

while using the sample API code

 

 

Public Sub SketchCurves()
' Create a new part.
Dim partDoc As PartDocument
Set partDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
Dim partDef As PartComponentDefinition
Set partDef = partDoc.ComponentDefinition

' Create a 2D sketch on the X-Y plane.
Dim sketch1 As PlanarSketch
Set sketch1 = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

' Create a spline based on control points.
Dim pnts As ObjectCollection
Set pnts = ThisApplication.TransientObjects.CreateObjectCollection
Call pnts.Add(tg.CreatePoint2d(2, 0))
Call pnts.Add(tg.CreatePoint2d(4, 1))
Call pnts.Add(tg.CreatePoint2d(4, 2))
Call pnts.Add(tg.CreatePoint2d(6, 3))
Call pnts.Add(tg.CreatePoint2d(8, 1))
Dim controlPointSpline As SketchControlPointSpline
Set controlPointSpline = sketch1.SketchControlPointSplines.Add(pnts)

' Create a 2D sketch on the Y-Z plane.
Dim sketch2 As PlanarSketch
Set sketch2 = partDef.Sketches.Add(partDef.WorkPlanes.Item(1))

' Create a spline based on an equation.
Dim equationCurve As SketchEquationCurve
Set equationCurve = sketch2.SketchEquationCurves.Add(kParametric, kCartesian, _
".001*t * cos(t)", ".001*t * sin(t)", 0, 360 * 3)

' Create a 3D sketch.
Dim sketch3 As sketch3D
Set sketch3 = partDef.Sketches3D.Add

' Create a 3D spline based on control points.
Set pnts = ThisApplication.TransientObjects.CreateObjectCollection
Call pnts.Add(tg.CreatePoint(10, 0, 0))
Call pnts.Add(tg.CreatePoint(12, 1, 3))
Call pnts.Add(tg.CreatePoint(12, 2, -5))
Call pnts.Add(tg.CreatePoint(14, 3, 2))
Call pnts.Add(tg.CreatePoint(16, 1, -3))
Dim controlPointSpline2 As SketchControlPointSpline3D
Set controlPointSpline2 = sketch3.SketchControlPointSplines3D.Add(pnts)

' Create a 3D spline based on an equation.
Dim equationCurve2 As SketchEquationCurve3D
Set equationCurve2 = sketch3.SketchEquationCurves3D.Add(kCartesian, _
".001*t * cos(t) + 8", ".001*t * sin(t)", "0.002*t", 0, 360 * 3)

ThisApplication.ActiveView.Fit

' Extrude the 2d curves.
Dim prof As Profile
Set prof = sketch1.Profiles.AddForSurface(controlPointSpline)
Dim extrudeDef As ExtrudeDefinition
Set extrudeDef = partDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(prof, kSurfaceOperation)
Call extrudeDef.SetDistanceExtent(6, kSymmetricExtentDirection)
Dim extrude1 As ExtrudeFeature
Set extrude1 = partDef.Features.ExtrudeFeatures.Add(extrudeDef)

' Change the work surface to not be transparent.
Dim surf As WorkSurface
Set surf = extrude1.SurfaceBodies.Item(1).Parent
surf.Translucent = False

Set prof = sketch2.Profiles.AddForSurface(equationCurve)
Set extrudeDef = partDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(prof, kSurfaceOperation)
Call extrudeDef.SetDistanceExtent(9, kPositiveExtentDirection)
Dim extrude2 As ExtrudeFeature
Set extrude2 = partDef.Features.ExtrudeFeatures.Add(extrudeDef)

' Create a new sketch and an intersection curve.
Dim interSketch As sketch3D
Set interSketch = partDef.Sketches3D.Add

Call interSketch.IntersectionCurves.Add(extrude1.SurfaceBodies.Item(1), extrude2.SurfaceBodies.Item(1))
End Sub

 

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes
Accepted solutions (1)
3,683 Views
10 Replies
Replies (10)
Message 2 of 11

ravikmb5
Collaborator
Collaborator

Has Anyone has tried this code to Run

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes
Message 3 of 11

JDMather
Consultant
Consultant

You might get faster response over here

http://forums.autodesk.com/t5/inventor-customization/bd-p/120

 

many people are on Holiday till next week.


-----------------------------------------------------------------------------------------
Autodesk Inventor 2019 Certified Professional
Autodesk AutoCAD 2013 Certified Professional
Certified SolidWorks Professional


0 Likes
Message 4 of 11

ekinsb
Alumni
Alumni

I just tried running the sample code and it's running without any problems.  I'm using Inventor 2015 but would expect it to run in Inventor 2014 too.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 11

ravikmb5
Collaborator
Collaborator

These are the Errors

When i Use Sample Code

 

SketchEquation Curve1.png

 

 

When i Click on Debug

 

it HighLights Sketch Curves

 

SketchEquation Curve.png

 

 

 

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes
Message 6 of 11

ravikmb5
Collaborator
Collaborator

i am Using Inventor 2015 & x64 bit Machine

 

Thanks for your Response

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes
Message 7 of 11

ekinsb
Alumni
Alumni

Strange.  I'm also using Inventor 2015 on a 64-bit machine.  I noticed in your picture that there's not any indentation in your code.  When I copy and paste the sample I get all of the indentation.  The indentation shouldn't matter but it makes me wonder if something else is different in your code than the original sample.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 8 of 11

ravikmb5
Collaborator
Collaborator

Here is an Video

Which is Showing Errors

 

 

 

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





0 Likes
Message 9 of 11

ekinsb
Alumni
Alumni

Very strange.  I did the exact same thing but it runs fine.  Can some others seeing this please try running the "Control point, equation, and interaction curve creation" sample and see if it works for you?  It's available in the programming help as shown below.  I can't think of any reason why it would work on one computer and not another.

 

 CurveSample.png

 

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 10 of 11

ravikmb5
Collaborator
Collaborator
Accepted solution

Figured it out 

 

Dim partDoc As PartDocument
Set partDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))

 

was set o default mm units

 

if i change my default template to inches it works

or if ichange units directly in code 

it works

 

The below code Works if it is Default mm Units

 

Spoiler
Public Sub SketchCurves()
' Create a new part.
Dim partDoc As PartDocument
Set partDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
Dim partDef As PartComponentDefinition
Set partDef = partDoc.ComponentDefinition

' Create a 2D sketch on the X-Y plane.
Dim sketch1 As PlanarSketch
Set sketch1 = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))

Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry

' Create a spline based on control points.
Dim pnts As ObjectCollection
Set pnts = ThisApplication.TransientObjects.CreateObjectCollection
Call pnts.Add(tg.CreatePoint2d(0.2, 0))
Call pnts.Add(tg.CreatePoint2d(0.4, 0.1))
Call pnts.Add(tg.CreatePoint2d(0.4, 0.2))
Call pnts.Add(tg.CreatePoint2d(0.6, 0.3))
Call pnts.Add(tg.CreatePoint2d(0.8, 0.1))
Dim controlPointSpline As SketchControlPointSpline
Set controlPointSpline = sketch1.SketchControlPointSplines.Add(pnts)

' Create a 2D sketch on the Y-Z plane.
Dim sketch2 As PlanarSketch
Set sketch2 = partDef.Sketches.Add(partDef.WorkPlanes.Item(1))

' Create a spline based on an equation.
Dim equationCurve As SketchEquationCurve
Set equationCurve = sketch2.SketchEquationCurves.Add(kParametric, kCartesian, _
"1 mm* 0.0254 ul * t * cos(1 deg * t)", "1 mm * 0.0254 ul * t * sin(1 deg * t)", 0, "360 ul * 3 ul")

' Set equationCurve = sketch2.SketchEquationCurve

' Create a 3D sketch.
Dim sketch3 As Sketch3D
Set sketch3 = partDef.Sketches3D.Add

' Create a 3D spline based on control points.
Set pnts = ThisApplication.TransientObjects.CreateObjectCollection
Call pnts.Add(tg.CreatePoint(0.1, 0, 0))
Call pnts.Add(tg.CreatePoint(0.12, 0.1, 0.3))
Call pnts.Add(tg.CreatePoint(0.12, 0.2, -0.5))
Call pnts.Add(tg.CreatePoint(0.14, 0.3, 0.2))
Call pnts.Add(tg.CreatePoint(0.16, 0.1, -0.3))
Dim controlPointSpline2 As SketchControlPointSpline3D
Set controlPointSpline2 = sketch3.SketchControlPointSplines3D.Add(pnts)

' Create a 3D spline based on an equation.
Dim equationCurve2 As SketchEquationCurve3D
Set equationCurve2 = sketch3.SketchEquationCurves3D.Add(kCartesian, _
"1 mm * 0.0254 ul * t * cos(1 deg * t) + 1 mm * 8 ul", "1 mm * 0.0254 ul * t * sin(1 deg * t)", " 1mm * 0.0508 * t", 0, "360 * 3")

ThisApplication.ActiveView.Fit

' Extrude the 2d curves.
Dim prof As Profile
Set prof = sketch1.Profiles.AddForSurface(controlPointSpline)
Dim extrudeDef As ExtrudeDefinition
Set extrudeDef = partDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(prof, kSurfaceOperation)
Call extrudeDef.SetDistanceExtent(0.6, kSymmetricExtentDirection)
Dim extrude1 As ExtrudeFeature
Set extrude1 = partDef.Features.ExtrudeFeatures.Add(extrudeDef)

' Change the work surface to not be transparent.
Dim surf(0 To 2) As WorkSurface
Set surf1 = extrude1.SurfaceBodies.Item(1).Parent
surf1.Translucent = True

Set prof = sketch2.Profiles.AddForSurface(equationCurve)
Set extrudeDef = partDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(prof, kSurfaceOperation)
Call extrudeDef.SetDistanceExtent(0.9, kPositiveExtentDirection)
Dim extrude2 As ExtrudeFeature
Set extrude2 = partDef.Features.ExtrudeFeatures.Add(extrudeDef)


Set surf2 = extrude2.SurfaceBodies.Item(1).Parent
surf2.Translucent = True

' Create a new sketch and an intersection curve.
Dim interSketch As Sketch3D
Set interSketch = partDef.Sketches3D.Add

Call interSketch.IntersectionCurves.Add(extrude1.SurfaceBodies.Item(1), extrude2.SurfaceBodies.Item(1))
End Sub


 

Sketch Curves.png

 

 

Thanks brain

Thanks For Your Quick Response

 

My Suggestion to Autodesk

They Should mention in each programme on what units this example is built in

 

 

Please mark this response as Problem Solved if it answers your question.
----------------------------------------------------------------------------------------------
Ravi Kumar MB,
HP Z220 SFF Workstation
Autodesk Inventor Certified professional 2016
Email: ravikmb5@gmail.com





Message 11 of 11

ekinsb
Alumni
Alumni

I'm glad you figured it out.  It's usually simple once you know the answer.  Checking to see if a sample is unit dependent is something we need to keep in mind.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes