Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBA Difference executing Inside or Outside sketch

2 REPLIES 2
Reply
Message 1 of 3
ArjanDijk
342 Views, 2 Replies

VBA Difference executing Inside or Outside sketch

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.


2 REPLIES 2
Message 2 of 3
YuhanZhang
in reply to: ArjanDijk

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.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 3
t_remal
in reply to: ArjanDijk

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report