Mirror Sketch Geometry

In the Sketch environment there is a Mirror command in the Pattern palette:

Sketch_Mirror4

Unfortunately, its functionality is not directly exposed in the API
However, we can check what it does and try to replicate it.

When I mirror sketch geometry, I can see that the Mirror command basically copies all the sketch points and lines, and then sets up a symmetry constraint between the original and copied sketch points.

Sketch_Mirror3

Here is a VBA sample which achieves this for sketch lines – for other geometry you would have to implement the functionality in a similar fashion:

Function CopyAndConstrainPoint(pt As SketchPoint, ln As SketchLine, pts As Dictionary) As SketchPoint
' The sketch we are working in
Dim sk As PlanarSketch
Set sk = pt.Parent
' Check if we already copied this
' sketch point
If Not pts.Exists(pt) Then
' Create new point
Set CopyAndConstrainPoint = sk.SketchPoints.Add(pt.Geometry, False)
Call pts.Add(pt, CopyAndConstrainPoint)
' Constrain it
Call sk.GeometricConstraints.AddSymmetry(pt, CopyAndConstrainPoint, ln)
Else
Set CopyAndConstrainPoint = pts(pt)
End If
End Function
Sub MirrorSketchLines()
Dim selSet As SelectSet
Set selSet = ThisApplication.ActiveDocument.SelectSet
' The first selected item needs to be the
' mirror line
Dim mirrorLine As SketchLine
Set mirrorLine = selSet(1)
' The sketch we are working in
Dim sk As PlanarSketch
Set sk = mirrorLine.Parent
' Let's get the lines we need to mirror
' We need to copy the lines, the sketch points
' and set up a symmetry constraint between the
' points and the mirror line
Dim mirroredSketchPoints As New Dictionary
Dim sketchLines As ObjectCollection
Set sketchLines = ThisApplication.TransientObjects.CreateObjectCollection()
' First store the selection set
' because it might get reset when adding new
' sketch entities and setting up constraints
Dim i As Integer
For i = 2 To selSet.Count
If TypeOf selSet(i) Is SketchLine Then
Call sketchLines.Add(selSet(i))
End If
Next
Dim line As SketchLine
For Each line In sketchLines
' Get the copied points
Dim sp As SketchPoint
Set sp = CopyAndConstrainPoint(line.StartSketchPoint, mirrorLine, mirroredSketchPoints)
Dim ep As SketchPoint
Set ep = CopyAndConstrainPoint(line.EndSketchPoint, mirrorLine, mirroredSketchPoints)
' Create the new line using the copied and constrained
' sketch points
Call sk.sketchLines.AddByTwoPoints(sp, ep)
Next
End Sub

When running it, make sure that you first select the mirror line, and then the rest of the geometry:

Sketch_Mirror2

-Adam


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading