Inventor Customization

Active Contributor
Posts: 40
Registered: ‎03-03-2010
Message 1 of 4 (203 Views)

Any ideas / advice why this macro does not work

203 Views, 3 Replies
10-03-2012 01:48 AM

Hello All, any input would be much apprciated. 


The macro crashes on the second last line:


"Call oSketch.DimensionConstraints.AddOffset(olines(1), oLine2, oDimCoords(1), False, False)"


Here is the full macro code:


Option Explicit
Dim oDoc As PartDocument
Dim oDef As PartComponentDefinition
Dim oApp As Inventor.Application

Const Mylar_X_Length = 80 'mm
Const Mylar_Y_Length = 80 'mm
Const Mylar_Thickness = 20 'mm
Const Mylar_Width = 20 'mm
Const Mylar_Fillet = 6 'mm
Const Mylar_MCAllow = 3 'mm


Public Sub L_Mylar()

Dim oSketch As PlanarSketch

Set oDoc = ThisApplication.Documents.Add(kPartDocumentObject, "Z:\TEMPLATES\INVENTOR\Sheet Metal (mm).ipt", True)
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
Dim oCoords(1 To 8) As Point2d
Dim oDimCoords(1 To 5) As Point2d
Dim oArc As SketchArc
Dim oLine As SketchLine
Dim olines(1 To 6)
Dim oLine2 As SketchLine

Set oDef = oDoc.ComponentDefinition

Set oCoords(1) = oTransGeom.CreatePoint2d(0, 0)
Set oCoords(2) = oTransGeom.CreatePoint2d(0, Mylar_Y_Length / 10)
Set oCoords(3) = oTransGeom.CreatePoint2d(Mylar_X_Length / 10, Mylar_Y_Length / 10)
Set oCoords(4) = oTransGeom.CreatePoint2d(Mylar_X_Length / 10, (Mylar_Y_Length - Mylar_Thickness) / 10)
Set oCoords(5) = oTransGeom.CreatePoint2d((Mylar_Thickness + Mylar_Fillet) / 10, (Mylar_Y_Length - Mylar_Thickness) / 10)
Set oCoords(6) = oTransGeom.CreatePoint2d((Mylar_Thickness + Mylar_Fillet) / 10, (Mylar_Y_Length - Mylar_Thickness - Mylar_Fillet) / 10)
Set oCoords(7) = oTransGeom.CreatePoint2d((Mylar_Thickness) / 10, (Mylar_Y_Length - Mylar_Thickness - Mylar_Fillet) / 10)
Set oCoords(8) = oTransGeom.CreatePoint2d((Mylar_Thickness) / 10, 0)

Set oSketch = oDef.Sketches.Add(oDef.WorkPlanes.Item("XY Plane"))
oSketch.Name = "SK_00 - PROFILE"

Set olines(1) = oSketch.SketchLines.AddByTwoPoints(oCoords(1), oCoords(2))
Set olines(2) = oSketch.SketchLines.AddByTwoPoints(olines(1).EndSketchPoint, oCoords(3))
Set olines(3) = oSketch.SketchLines.AddByTwoPoints(olines(2).EndSketchPoint, oCoords(4))
Set olines(4) = oSketch.SketchLines.AddByTwoPoints(olines(3).EndSketchPoint, oCoords(5))
Set oArc = oSketch.SketchArcs.AddByCenterStartEndPoint(oCoords(6), olines(4).EndSketchPoint, oCoords(7))
Set olines(5) = oSketch.SketchLines.AddByTwoPoints(oArc.EndSketchPoint, oCoords(8))
Set olines(6) = oSketch.SketchLines.AddByTwoPoints(olines(5).EndSketchPoint, olines(1).StartSketchPoint)

Call oSketch.GeometricConstraints.AddPerpendicular(olines(1), olines(2))
Call oSketch.GeometricConstraints.AddParallel(olines(1), olines(3))
Call oSketch.GeometricConstraints.AddParallel(olines(1), olines(5))
Call oSketch.GeometricConstraints.AddParallel(olines(2), olines(4))
Call oSketch.GeometricConstraints.AddParallel(olines(2), olines(6))
Call oSketch.GeometricConstraints.AddTangent(olines(4), oArc)
Call oSketch.GeometricConstraints.AddTangent(olines(5), oArc)
Call oSketch.GeometricConstraints.AddGround(olines(1).StartSketchPoint)
Call oSketch.GeometricConstraints.AddVertical(olines(1))

Set oDimCoords(1) = oTransGeom.CreatePoint2d(-0.5, oCoords(2).Y / 2)
Set oDimCoords(2) = oTransGeom.CreatePoint2d(oCoords(3).X / 2, oCoords(3).Y + 0.5)
Set oDimCoords(3) = oTransGeom.CreatePoint2d(oCoords(3).X + 0.5, oCoords(3).Y - (Mylar_Thickness / 2) / 10)
Set oDimCoords(4) = oTransGeom.CreatePoint2d(Mylar_Thickness / 10 + 0.5, oCoords(3).Y - (Mylar_Thickness / 2) / 10 - 0.5)
Set oDimCoords(5) = oTransGeom.CreatePoint2d((Mylar_Thickness / 2) / 10, -0.5)

Call oSketch.DimensionConstraints.AddOffset(olines(6), olines(2), oDimCoords(1), False, False)
Call oSketch.DimensionConstraints.AddOffset(olines(1), olines(3), oDimCoords(2), False, False)
Call oSketch.DimensionConstraints.AddOffset(olines(2), olines(4), oDimCoords(3), False, False)
Call oSketch.DimensionConstraints.AddRadius(oArc, oDimCoords(4), False)
Call oSketch.DimensionConstraints.AddOffset(olines(1), olines(5), oDimCoords(5), False, False)

Dim oCollection As ObjectCollection
Set oCollection = ThisApplication.TransientObjects.CreateObjectCollection
oCollection.Add olines(1)
oCollection.Add olines(2)
oCollection.Add olines(3)
oCollection.Add olines(4)
oCollection.Add oArc
oCollection.Add olines(5)
oCollection.Add olines(6)

Call oSketch.OffsetSketchEntitiesUsingDistance(oCollection, Mylar_MCAllow / 10, False, False, True)
Set oLine2 = oCollection.Item(1)
Call oSketch.DimensionConstraints.AddOffset(olines(1), oLine2, oDimCoords(1), False, False)

End Sub


Windows 7 Pro (X64)
Intel(R) core (TM) i7-2600 CPU @ 3.40GHz
16.0 GB RAM
Nvidia Quadro 600
Autodesk Inventor 2013 Professional Ultimate Design Suite
Valued Contributor
Posts: 76
Registered: ‎07-26-2007
Message 2 of 4 (174 Views)

Re: Any ideas / advice why this macro does not work

10-04-2012 06:01 AM in reply to: rvangend

Hi Rob,


I can help a little. oLine2 = oCollection.Item(1), is refering the your original set of lines, not the offset ones. Therefore you're trying to add a dimension from oLines(1) to oLines(1).


I'm afraid I haven't got any spare time at the moment to look at this further. Hope this gives you a starting point at least.


Kind regards



If this post helps you, please give kudos.
Active Contributor
Posts: 40
Registered: ‎03-03-2010
Message 3 of 4 (169 Views)

Re: Any ideas / advice why this macro does not work

10-04-2012 07:14 AM in reply to: rvangend

Hello Jon,


I realised that after I posted and should probably have deleted the post.


But, Is there any way to iterate the offset group and find the line that matches olines(1) (or oline(2) and so on)  so that I can add a dimension?


Thanks for you input.




Windows 7 Pro (X64)
Intel(R) core (TM) i7-2600 CPU @ 3.40GHz
16.0 GB RAM
Nvidia Quadro 600
Autodesk Inventor 2013 Professional Ultimate Design Suite
ADN Support Specialist
Posts: 1,264
Registered: ‎06-12-2011
Message 4 of 4 (126 Views)

Re: Any ideas / advice why this macro does not work

10-17-2012 07:23 PM in reply to: rvangend



When you said 'offset group', did you meant by 'oCollection' in the code? A collection can be iterated like below:


Dim oEachObj as Object

For Each oEachObj in oCollection




If I misunderstood, please feel free to elaborate your question

Xiaodong Liang
Developer Technical Services
Autodesk Developer Network

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community

Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor