Dimension a rectangle

Dimension a rectangle

Anonymous
Not applicable
1,092 Views
2 Replies
Message 1 of 3

Dimension a rectangle

Anonymous
Not applicable

 Can anyone help?? I have managed to creat (copy !) a sub which draws a rectangle with input from a form - Length and Width -- with the code below

 

' Draw a rectangle with the corner at (0,0)
    Dim oRectangleLines As SketchEntitiesEnumerator
     Set oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
                                oTransGeom.CreatePoint2d(0, 0), _
                                oTransGeom.CreatePoint2d(Length, Width))

 

I am having a problem putting dimensions on the sketch so those dims appear in the parameters.

I have managed to do it with a single line ( see code below) but cannot fathom out what part of the rectangle I can use as points to position the start and finish of the dimension line.

 

Note...  oLines(1) was the line drawn and oTextPt(1) was defined before the code below to position the dim text

 

' Create a length dimension


    Dim oLengthDim(1 To 3) As DimensionConstraint
    
   'Set oLengthDim(1) = oSketch.DimensionConstraints.AddTwoPointDistance(oLines(1).StartSketchPoint, _
   'oLines(1).EndSketchPoint, kAlignedDim, oTextPt(1), False)

 

Thanks in anticipation Dave (Newbie)

 

 

 

Accepted solutions (1)
1,093 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni
Accepted solution

Creating a rectangle just creates four lines and various constraints (parallel, perpendicular, coincident, etc.) to make it maintain its rectangular shape.  The four lines that are created are returned in the SketchEntitiesEnumerator collection.  You can then get access to each line through this collection.  It was a matter of some trial and error to find out which line was which in the collection.  These should be consistent as along as your draw the rectangle in the same way, lower-left point to upper-right point.

 

Public Sub DimensionSketch()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
   
    ' Create a new sketch on the x-y work plane.
    Dim sketch As PlanarSketch
    Set sketch = partDoc.ComponentDefinition.Sketches.Add( _
                 partDoc.ComponentDefinition.WorkPlanes.Item(3))
   
    Dim tg As TransientGeometry
    Set tg = ThisApplication.TransientGeometry
   
    ' Draw a rectangle with the corner at (0,0)
    Dim width As Double
    width = 5
    Dim height As Double
    height = 2
    Dim rectangleLines As SketchEntitiesEnumerator
    Set rectangleLines = sketch.SketchLines.AddAsTwoPointRectangle( _
                                tg.CreatePoint2d(0, 0), _
                                tg.CreatePoint2d(width, height))

    ' Get the third line from the collection returned.
    ' This will be the top line.
    Dim dimLine As SketchLine
    Set dimLine = rectangleLines.Item(3)
   
    ' Create a dimension between the end points of the line.
    Call sketch.DimensionConstraints.AddTwoPointDistance( _
                       dimLine.StartSketchPoint, dimLine.EndSketchPoint, _
                       kAlignedDim, tg.CreatePoint2d(width / 2, height + 1))
   
    ' Get the fourth line from the collection returned.
    ' This will be the left line.
    Set dimLine = rectangleLines.Item(4)
   
    ' Create a dimension between the end points of the line.
    Call sketch.DimensionConstraints.AddTwoPointDistance( _
                       dimLine.StartSketchPoint, dimLine.EndSketchPoint, _
                       kAlignedDim, tg.CreatePoint2d(-1, height / 2))
End Sub


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

Anonymous
Not applicable

Hi Brian.  Thankyou very much for that. Yes a bit of tweaking and it works with my previous code, and the values appear in the parameters window nicely. This will be used to draw a furniture panel with pvc edges.  I now have to progress to developing my form probably with a graphic of a rectangle and tick boxes positioned on each edge of the rectangle to designate which edge has an edging applied.  A bit of code will reduce the panel dimensions by the thickness of the edging where appropriate so the panel will be cut and end up the right size when edged up..hopefully !  Kind Regards Dave.

0 Likes