Centre Point Rectangle

Centre Point Rectangle

robmatthews
Collaborator Collaborator
1,121 Views
4 Replies
Message 1 of 5

Centre Point Rectangle

robmatthews
Collaborator
Collaborator

Hi there.

 

I have a kludged (probably) sub that creates a centre-point rectangle based at a projection of the origin.  It is quite klunky, I think, and has several limitations:

- I don't know how to grab the origin projection if it already exists, so i just check for it and deleted it. I'm sure i should be able to create a reference to it at that time.

- It probably would be nice to also use this code for a user-selected location for the rectangle. I would want the user just to pick the centre point, and a corner point. Can anyone help me with these two things?

 

(As I said, the code is probably quite clunky, and if you'd like to clean it up, that'd be fine too 😉

 

(I'm using IV9, not 2009. Yes, i know. And I created this from study and application of the help-file.)

 

Cheers.

 

Sub InitialRectangle()
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
    ' Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Item(oCompDef.Sketches.count)
    ' Set a reference to the transient geometry object.
Dim oLines(1 To 4) As SketchLine
Dim oRectLines As SketchEntitiesEnumerator
Dim count As Integer
Dim XVal As Integer
Dim YVal As Integer
XVal = 3
YVal = 2
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
'check to see if the origin has already been projected. If so, delete it.
If oSketch.SketchPoints.count > 0 Then
    For count = 1 To oSketch.SketchPoints.count
        If oSketch.SketchPoints(count).Geometry.X = 0 And oSketch.SketchPoints(count).Geometry.Y = 0 Then
            oSketch.SketchPoints(count).Delete
        End If
        count = count + 1
    Next
End If
With oTransGeom
    Set oRectLines = oSketch.SketchLines.AddAsTwoPointRectangle(.CreatePoint2d(-XVal, -YVal), .CreatePoint2d(XVal, YVal))
    Dim oDiagonalLine As SketchLine
    Set oDiagonalLine = oSketch.SketchLines.AddByTwoPoints(.CreatePoint2d(XVal, -YVal), .CreatePoint2d(-XVal, YVal))
    oDiagonalLine.Construction = True
    Dim oOrigin As WorkPoint
    Set oOrigin = oCompDef.WorkPoints.Item(1)
    Dim oOriginPoint As SketchPoint
'******************* update this line *********************
    Set oOriginPoint = oSketch.AddByProjectingEntity(oOrigin)
'******************* update this line *********************
    With oSketch.GeometricConstraints
        Call .AddCoincident(oDiagonalLine.StartSketchPoint, oRectLines(1))
        Call .AddCoincident(oDiagonalLine.StartSketchPoint, oRectLines(2))
        Call .AddCoincident(oDiagonalLine.EndSketchPoint, oRectLines(3))
        Call .AddCoincident(oDiagonalLine.EndSketchPoint, oRectLines(4))
        Call .AddMidpoint(oOriginPoint, oDiagonalLine)
    End With
End With
End Sub

 

=============================================
This is my signature, not part of my post.
0 Likes
1,122 Views
4 Replies
Replies (4)
Message 2 of 5

robmatthews
Collaborator
Collaborator

Right. I've tidied it up a fair bit, I'm not deleting projected origin point any more, so any existing geometry that might be constrained to that is not set loose; there are more comments, and I think it's a lot better structured.

 

However, it's still not a user-picking centre-point rectangle, hence the name. My feeling is that it's best used as a start for the majority of non-circular parts, and that's what we use it for here.

 

If anyone can simply show how to grab user-chosen points, I'm all ears...

 

Sub InitialRectangle()
Dim oPartDoc As PartDocument 'for the active part
Dim oCompDef As PartComponentDefinition
Dim oSketch As PlanarSketch
Dim oLines(1 To 4) As SketchLine 'each of the 4 sides of the rectangle
Dim oRectLines As SketchEntitiesEnumerator
Dim count As Integer
Dim XVal As Integer
Dim YVal As Integer
Dim bFoundOrigin As Boolean
Dim oTransGeom As TransientGeometry
Dim oDiagonalLine As SketchLine 'the diagonal line in the rectangle
Dim oOriginPoint As SketchPoint
Dim oOrigin As WorkPoint

Set oPartDoc = ThisApplication.ActiveDocument
If oPartDoc.SketchActive = True Then 'check to ensure you have a sketch active
    Set oCompDef = oPartDoc.ComponentDefinition ' Set a reference to the component definition.
    Set oSketch = oCompDef.Sketches.Item(oCompDef.Sketches.count)
    XVal = 3 'set this to half the horizontal size you want the rectangle to be
    YVal = 2 'set this to half the vertical size you want the rectangle to be
    bFoundOrigin = False
    Set oTransGeom = ThisApplication.TransientGeometry 'Set a reference to the transient geometry object.
If oSketch.SketchPoints.count > 0 Then 'try to find if the origin has been projected already
    For count = 1 To oSketch.SketchPoints.count
        If oSketch.SketchPoints(count).Geometry.X = 0 And oSketch.SketchPoints(count).Geometry.Y = 0 Then
            bFoundOrigin = True 'if you have found the origin, keep a ref to it
            Exit For
        End If
    Next
End If
With oTransGeom 'Draw the 2-point rectangle
    Set oRectLines = oSketch.SketchLines.AddAsTwoPointRectangle(.CreatePoint2d(-XVal, -YVal), .CreatePoint2d(XVal, YVal))
    Set oDiagonalLine = oSketch.SketchLines.AddByTwoPoints(.CreatePoint2d(XVal, -YVal), .CreatePoint2d(-XVal, YVal))
    oDiagonalLine.Construction = True
    With oSketch.GeometricConstraints
        Call .AddCoincident(oDiagonalLine.StartSketchPoint, oRectLines(1))
        Call .AddCoincident(oDiagonalLine.StartSketchPoint, oRectLines(2))
        Call .AddCoincident(oDiagonalLine.EndSketchPoint, oRectLines(3))
        Call .AddCoincident(oDiagonalLine.EndSketchPoint, oRectLines(4))
    End With
    If bFoundOrigin = False Then
        Set oOrigin = oCompDef.WorkPoints.Item(1)
        Set oOriginPoint = oSketch.AddByProjectingEntity(oOrigin)
        With oSketch.GeometricConstraints
            Call .AddMidpoint(oOriginPoint, oDiagonalLine)
        End With
    Else
        With oSketch.GeometricConstraints
            Call .AddMidpoint(oSketch.SketchPoints(count), oDiagonalLine)
        End With
    End If
End With
Else
    MsgBox "There is no sketch active. Operation Terminated.", vbCritical
End If
End Sub

 

=============================================
This is my signature, not part of my post.
0 Likes
Message 3 of 5

ChrisAtherton
Advocate
Advocate

http://campaign.constantcontact.com/render?v=001wGH4ADbzDTv_32QT8jcgwstULKv1vw77n0JWjgTVur2eLYcxxV9U...

 

This has a download for a centre point tool.

Chris Atherton
IEng MIMechE BEng Hons
Design Automation Services Manager | Symetri
https://uk.linkedin.com/in/chrissoathe
0 Likes
Message 4 of 5

robmatthews
Collaborator
Collaborator

Thanks, I saw the discussion of that in the IV newsgroup, and it looks great. Unfortunately, we're waaaay back on v9 here, and besides that, I would like to know for my benefit how to return a user-chosen point on a face.

 

No big deal though, it's not a high priority for me.

 

Cheers.

=============================================
This is my signature, not part of my post.
0 Likes
Message 5 of 5

Qube-it
Advocate
Advocate

I'm not sure what the Inventor R9 API capabilities are, but there would be two possible ways to do what you're asking. 

 

One way would be through the PointInferenceEnabled property for the MouseEvents object.  If you set that to true, you will turn on the tracking and snapping that happens normally when you draw a sketch line.  It also collects information about what the user selects and stores it as an Enumerator in a collection called "PointInferences" (part of the MouseEvents object).  You can iterate through that collection (usually you will only need the first item in the collection) and set conditions on what to do for each PointInferencesEnumerator type (kPtAtIntersection, kPtOnCurve, kPtOnPt, KPtAtMidPoint).  I would recommend using a SelectCase statement to do this.

 

This is the cleanest way of interacting with user selection inside  a sketch, though I'm not sure if PointInferences was introduced before or after Inventor R9, so I would say look in the API help for it.  Just do a search for PointInference and it will pop up lots of stuff if it's in there.

 

The other way to get a point that a user selects is via the SelectEvents object through the InteractionEvents object.  I know that SelectEvents was in Inventor V9.  I would recommend reading through the help on this one.  It does a pretty good job of explaining how to use it.  Better than I could do. 

-Brian Hall-
0 Likes