Message 1 of 17
Looking for suggestions or improvements on sketch creating function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I use a 2 point center rectangle with points on the corners all the time for creating symmetrical holes centered on a part and have dreamt of making a script to do it for far too long. Finally found some time to make this script and as sketches are one of the more unknown areas of the API for me, I wanted to see if anyone has suggestions or better ways to accomplish this. This code is VBA and was done as a proof of concept, I'm going to clean it up better and move it over to my VB.net addin when I have more spare time.
Public Sub SketchCreation()
'Prompt to select face to create new sketch
Dim sketch As PlanarSketch
Set sketch = ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches.Add(ThisApplication.CommandManager.Pick(kPartFacePlanarFilter, "Pick face to add sketch"))
Dim tg As TransientGeometry
Set tg = ThisApplication.TransientGeometry
Dim oPoint As WorkPoint
Set oPoint = ThisApplication.ActiveDocument.ComponentDefinition.WorkPoints.Item(1)
'Try to get origin point
Dim oOriginSketchPoint As SketchPoint
Dim oSketchPoint As SketchPoint
For Each oSketchPoint In sketch.SketchPoints
If oSketchPoint.ReferencedEntity Is oPoint Then
Set oOriginSketchPoint = oSketchPoint
Exit For
End If
Next
' Create the sketch point for the origin, if it doesn't already exist.
If oOriginSketchPoint Is Nothing Then
' Project the origin point onto the sketch.
Set oOriginSketchPoint = sketch.AddByProjectingEntity(oPoint)
End If
'Get size of rectangle
X = CDbl(InputBox(Prompt:="X dimension.", Title:="X", Default:="1")) * 2.54
Y = CDbl(InputBox(Prompt:="Y dimension.", Title:="Y", Default:="1")) * 2.54
' Draw rectangles by center point.
Call sketch.SketchLines.AddAsTwoPointCenteredRectangle(oOriginSketchPoint, tg.CreatePoint2d(X / 2, Y / 2))
'Add dimensions to two sides of rectangle
Dim TextPoint As Point2d
Set TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(0, Y / 2 + 1)
Call sketch.DimensionConstraints.AddTwoPointDistance(sketch.SketchLines.Item(3).StartSketchPoint, sketch.SketchLines.Item(3).EndSketchPoint, kHorizontalDim, TextPoint)
Set TextPoint = ThisApplication.TransientGeometry.CreatePoint2d(X / 2 + 1, 0)
Call sketch.DimensionConstraints.AddTwoPointDistance(sketch.SketchLines.Item(2).StartSketchPoint, sketch.SketchLines.Item(2).EndSketchPoint, kVerticalDim, TextPoint)
'Add center points four corners of rectangle
Dim point As SketchPoint
For num = 1 To 4 Step 2
Set point = sketch.SketchPoints.Add(tg.CreatePoint2d(0, 0))
Call sketch.GeometricConstraints.AddCoincident(point, sketch.SketchLines.Item(num).StartSketchPoint)
Set point = sketch.SketchPoints.Add(tg.CreatePoint2d(0, 0))
Call sketch.GeometricConstraints.AddCoincident(point, sketch.SketchLines.Item(num).EndSketchPoint)
Next
End Sub
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
pball's Autodesk Forum Style
Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript