Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding dimension constraints to a sketch

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
NickBrege
2274 Views, 7 Replies

Adding dimension constraints to a sketch

I have a couple quick questions regarding the Inventor API.  I can create a sketch rectangle using this code:

 

        Dim oRectangleLines As SketchEntitiesEnumerator
        oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle(oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(10 * 2.54, 6 * 2.54))

Now how do I add dimension constraints to this rectangle? (both width & height)

 

Also, what is the default unit when creating sketch geometry?  It appears to be centimeters.  It there a way to change this, to inches for example?  Thanks...

7 REPLIES 7
Message 2 of 8
DRoam
in reply to: NickBrege

Here's a link to the API page with the available DimensionConstraint.Add... methods: DimensionConstraints Object.

 

It appears there's not a method for adding a dimension to a line itself, so you'll have to dimension between its endpoints instead. Here's a link to the API help page for that method: DimensionConstraints.AddTwoPointDistance Method.

 

You can follow the "Parent Object" links to find out how to get to this method.

 

Based on how that method works, you'll need to get the endpoints of each line that you want to dimension. To access the lines in your new rectangle, you can use:

 

 

Dim oSketchLine As Inventor.SketchLine
oSketchLine = oRectangleLines.Item(1) 'Enter a different item number to get a different line.

 

Once you have a line, here's a link to the help page for the SketchLine object: SketchLine Object. You can use the StartSketchPoint and EndSketchPoint methods to get the endpoints of your line.

 

You'll have to do this for 2 lines. I would guess that Item(1) and Item(2) would work, but you'll have to experiment to see.

 

The API reference manual is extremely handy for learning how to do new things in iLogic. Here's a link to the main page: Inventor API User's Manual. I'd recommend bookmarking it. Once you get to that page, expand the "Inventor API Reference Manual" node in the tree on the left, and then expand the "Objects" node. You can then use Ctrl+F to search for terms (like "Dimension" or "Sketch") to see what objects there are, how to get to them (by following the "Parent Object" or "Accessed From" links), and what methods/properties are available for them.

 

Hope that helps!

 

Message 3 of 8
DRoam
in reply to: NickBrege

Oh and regarding the units, yes, the iLogic working units (referred to as the "internal database units") are centimeters, and unfortunately there's no way to change this. Certain iLogic functions use the document units (like Parameter), but some of them use the internal database units.

 

Not surprisingly, there's an API help page for the "UnitsOfMeasure" object which has some handy tools for working with and converting between units: UnitsOfMeasure Object.

 

But in general, you'll have to do exactly what you did and multiply by 2.54 when assigning an inch value to your model, or divide by 2.54 when getting one.

Message 4 of 8
NickBrege
in reply to: DRoam

Thank you...

Message 5 of 8
NickBrege
in reply to: NickBrege

I got it to work.  Here is my code ... it might help someone in the future.

 

        Dim oSketchLine As Inventor.SketchLine
        oSketchLine = oRectangleLines.Item(1)
        oSketch.DimensionConstraints.AddTwoPointDistance(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, oTransGeom.CreatePoint2d(0, -2))
        oSketchLine = oRectangleLines.Item(4)
        oSketch.DimensionConstraints.AddTwoPointDistance(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, DimensionOrientationEnum.kVerticalDim, oTransGeom.CreatePoint2d(-2, 0))

Thanks again for the help...

Message 6 of 8
jewol64392
in reply to: NickBrege

Hi could you post the whole code including creating the rectangle with dimensioning in the sketch? Thank you
Message 7 of 8
jewol64392
in reply to: NickBrege

Hi could you post the whole code including creating the rectangle with dimensioning in the sketch? Thank you

Message 8 of 8
A.Acheson
in reply to: jewol64392

Hi @jewol64392 

Have you looked at the VBA samples in the API Help? Here is the sample for adding rectangles in VBA

the codes below are posted in descending starting with VBA sample at the bottom, conversion to VB.Net/ilogic and finally adding dimensions. 

 

Here is a rectangle creation with dimension constraint. It is untested so post any issues you find.

    ' Get the active part document.
    Dim partDoc As PartDocument = ThisApplication.ActiveDocument
    
	Dim partDef As PartComponentDefinition = partDoc.ComponentDefinition
    
    ' Create a new sketch.
    Dim sketch As PlanarSketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry = ThisApplication.TransientGeometry
   
    ' Draw rectangles by center point.
	Dim rectangleLines As SketchEntitiesEnumerator
    Dim rectangleLines = sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
   'Dim oRectangleLines = sketch.SketchLines.AddAsThreePointCenteredRectangle(tg.CreatePoint2d(20, 0), tg.CreatePoint2d(28, 3), tg.CreatePoint2d(24, 9))
	 
	 Dim recSketchLine As SketchLine 
	 recSketchLine = rectangleLines.Item(1)
     sketch.DimensionConstraints.AddTwoPointDistance(recSketchLine.StartSketchPoint, recSketchLine.EndSketchPoint, DimensionOrientationEnum.kHorizontalDim, oTransGeom.CreatePoint2d(0, -2))
     
	 recSketchLine = oRectangleLines.Item(4)
	 sketch.DimensionConstraints.AddTwoPointDistance(recSketchLine.StartSketchPoint, recSketchLine.EndSketchPoint, DimensionOrientationEnum.kVerticalDim, oTransGeom.CreatePoint2d(-2, 0))

    ThisApplication.ActiveView.Fit

 

 

Here is the VBA sample converted to VB.Net for use in ilogic editor with shortest possible method. Note you can remove all the set lines VBA required and declare and set the object variable in one line. 

    ' Get the active part document.
    Dim partDoc As PartDocument = ThisApplication.ActiveDocument
    
	Dim partDef As PartComponentDefinition = partDoc.ComponentDefinition
    
    ' Create a new sketch.
    Dim sketch As PlanarSketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry = ThisApplication.TransientGeometry
    
    ' Draw rectangles by center point.
    sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
    sketch.SketchLines.AddAsThreePointCenteredRectangle(tg.CreatePoint2d(20, 0), tg.CreatePoint2d(28, 3), tg.CreatePoint2d(24, 9))
    
    ThisApplication.ActiveView.Fit

Here is the VBA sample converted to VB.Net with minimum changes for use in ilogic editor

Sub Main
	SketchCreation()
End Sub

Public Sub SketchCreation()
    ' Get the active part document.
    Dim partDoc As PartDocument
	partDoc = ThisApplication.ActiveDocument
    
	Dim partDef As PartComponentDefinition
    partDef = partDoc.ComponentDefinition
    
    ' Create a new sketch.
    Dim sketch As PlanarSketch
    sketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry
    tg = ThisApplication.TransientGeometry
    
    ' Draw rectangles by center point.
    Call sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
    Call sketch.SketchLines.AddAsThreePointCenteredRectangle(tg.CreatePoint2d(20, 0), tg.CreatePoint2d(28, 3), tg.CreatePoint2d(24, 9))
    
    ThisApplication.ActiveView.Fit
End Sub

 

Here is the VBA version API Help

Public Sub SketchCreation()
    ' Get the active part document.
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    Dim partDef As PartComponentDefinition
    Set partDef = partDoc.ComponentDefinition
    
    ' Create a new sketch.
    Dim sketch As PlanarSketch
    Set sketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
    
    ' Draw rectangles by center point.
    Call sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
    Call sketch.SketchLines.AddAsThreePointCenteredRectangle(tg.CreatePoint2d(20, 0), tg.CreatePoint2d(28, 3), tg.CreatePoint2d(24, 9))
    
    ThisApplication.ActiveView.Fit
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report