Public Sub MoveSketchObjects() ' Check to make sure a sketch is open. If Not Typeof ThisApplication.ActiveEditObject Is Sketch Then MsgBox "A sketch must be active." Exit Sub End If ' Set a reference to the active sketch. Dim oSketch As Sketch Set oSketch = ThisApplication.ActiveEditObject ' Create a vector along the x-axis. Dim oVec As Vector2d Set oVec = ThisApplication.TransientGeometry.CreateVector2d(5, 0) Dim oSketchObjects As ObjectCollection Set oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection ' Get all entities in the sketch Dim oSketchEntity As SketchEntity For Each oSketchEntity In oSketch.SketchEntities oSketchObjects.Add oSketchEntity Next ' Move all sketch objects along x-axis by 5 units. ' This will move all the text boxes and images in ' the sketch as well since these have sketch lines ' as boundary geometry or a sketch point as an ' origin point. Call oSketch.MoveSketchObjects(oSketchObjects, oVec) End Sub
Hello. I'm trying to find out if I can execute move commands like the one in this example also witout having to open the sketch?
What code will I need in that case?
Inventor HSM and Fusion 360 CAM trainer and postprocessor builder in the Netherlands and Belgium.
In part/Assembly document you can edit sketch entities without activating it, but after editing the sketch you'd better to call Document.Update to make sure the edit takes effect to update the graphic window. But for drawing sketch you have to activate it before you can edit the entities in it. Hope this helps.
Hello,
I think you cannot move sketch objects without opening the sketch first.
However, you can open the sketch from VBA by calling oSketch.Edit and close it with oSketch.ExitEdit.
I changed your sub MoveSketchObjects to accept one parameter (the sketch to edit):
Public Sub MoveSketchObjects(oSketch As Sketch) ' Create a vector along the x-axis. Dim oVec As Vector2d Set oVec = ThisApplication.TransientGeometry.CreateVector2d(5, 0) Dim oSketchObjects As ObjectCollection Set oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection ' Get all entities in the sketch Dim oSketchEntity As SketchEntity For Each oSketchEntity In oSketch.SketchEntities oSketchObjects.Add oSketchEntity Next ' Open the sketch for editing. Call oSketch.Edit ' Move all sketch objects along x-axis by 5 units. ' This will move all the text boxes and images in ' the sketch as well since these have sketch lines ' as boundary geometry or a sketch point as an ' origin point. Call oSketch.MoveSketchObjects(oSketchObjects, oVec) ' Close the sketch. Call oSketch.ExitEdit End Sub
As you can see, Edit is called before MovesketchObjects and ExitEdit after.
You still need to get the parameter (sketch) from somewhere. Which sketch would you like to change? Selected one? All of them?
Here's some code for selected sketches:
Public Sub MoveSelectedSketches() Dim oSketch As Sketch For Each oSelection In ThisDocument.SelectSet If TypeOf oSelection Is Sketch Then Set oSketch = oSelection Call MoveSketchObjects(oSketch) End If Next End Sub
Hope that helps!
Regards,
Lubomir