Trying to use VB.NET to run a native command. What am I missing?

Trying to use VB.NET to run a native command. What am I missing?

Anonymous
Not applicable
1,516 Views
12 Replies
Message 1 of 13

Trying to use VB.NET to run a native command. What am I missing?

Anonymous
Not applicable

Hi all. I'm trying to write a plug-in that automatically slices the graphics when a sketch is created and edited. 

Here's the code I'm using:EnableCmd.PNG

 

The MessageBox displays as expected for NEW sketches, so we know the code works. However, the code to run the command "SketchSliceGraphicsCmd" does not. What's missing in order to run the command? What is the process of using a built in command called? Raising an Event? Handling a Trigger?

 

Lastly, the sub runs "OnNewSketch". I do not see a "OnSketchEdit". What is the equivalent event for triggering an event when a sketch is edited?

0 Likes
Accepted solutions (2)
1,517 Views
12 Replies
Replies (12)
Message 2 of 13

JamieVJohnson2
Collaborator
Collaborator

perhaps Execute or Execute2

            Dim cd As ControlDefinition = invApp.CommandManager.ControlDefinitions.Item("SketchSliceGraphicsCmd")
            cd.Execute()

It looks to me like you were setting up a command to run as Ilogic runs during an event.  While attempting to just actually run a command instead...  All confused really.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 3 of 13

Anonymous
Not applicable

Thanks for the quick reply.

 

Unfortunately the command still didn't run.

This time I tried putting a watch on "Item" and it came back with "Item is not declared." However, watching "CommandManager" it's possible to navigate down to ControlDefinition, but "Item" is not available when expanded.

 

But watching "cd" does say that the DescriptionText is matches up with the command. 

0 Likes
Message 4 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Hoping that suggestion in below link may be helpful.

 

https://modthemachine.typepad.com/my_weblog/2009/03/running-commands-using-the-api.html

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 13

Anonymous
Not applicable

@chandra.shekar.g 

 

Thank you for the information, I'll definitely be using it.

 

However, I'm still stuck in my predicament: the message box will display, but the command just before it doesn't. 

 

I've attached my code, would you mind running it and seeing if you can see anything that I could be missing?

0 Likes
Message 6 of 13

Anonymous
Not applicable

After doing some tinkering, I discovered that the code works as intended when MessageBox.Show("New Sketch") is prompted out, or placed before cd.Execute()

 

Example:

 

MessageBox.Show("New Sketch")
                    Dim cd As ControlDefinition = m_inventorApplication.CommandManager. _
ControlDefinitions.Item("SketchSliceGraphicsCmd") cd.Execute()

On new sketch, I get a message box, then the graphics slice.

 

 

But this:

    Dim cd As ControlDefinition = m_inventorApplication.CommandManager. _
ControlDefinitions.Item("SketchSliceGraphicsCmd") cd.Execute() MessageBox.Show("New Sketch")

gives a message box only.

 

Any ideas why?

 

0 Likes
Message 7 of 13

JamieVJohnson2
Collaborator
Collaborator

Application focus.  Message box puts focus back into Inventor when you click OK (or whatever button you configure it to use).  Not expected behavior on Inventor's part, but the cd.Execute, may not be in the scope of the thread if the user clicks around, activating different threads (windows, documents, whatever), or the code may be changing focus to another thread (different document?) that that command has no effect upon.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
Message 8 of 13

Anonymous
Not applicable

@JamieVJohnson2 

 

Last week I gave this a go:

Capture.PNG

 

encountering the same problem. The snippet above is configured to handle ANY activated command, then I can step through the commands as they're activated to watch for the specific one I want to catch. When the program gets to "SketchSliceGraphicsCmd", it will do as expected, but the screen does not update. Even if breakstops aren't used, letting the program do it's thing, the focus does not appear to change.

 

 

0 Likes
Message 9 of 13

bradeneuropeArthur
Mentor
Mentor
Accepted solution

The code is running before the sketch is ready.

you run this command on "PartNewSkechCreate", but this means not that the workplane and the sketch are defined.

So the "SketchSliceGraphicsCmd" will not run for this sketch because in is in preparation, but not ready.

 

You need to run this command after the sketch workplane is defined!!

 

Regards,

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 10 of 13

YuhanZhang
Autodesk
Autodesk

I have not tried your code, but I suggest that you can test it as below, in your OnNewSketch event, in kAfter you can firstly call Sketch.Edit to make sure your document is now in sketch environment, then execute the command(SketchSliceGraphicsCmd). 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.

0 Likes
Message 11 of 13

Anonymous
Not applicable

@bradeneuropeArthur 

 

Thanks for the advice! After stepping though the program, you are correct: the sketch is not ready. Unfortunately "PartNewSketchCreate" is not an event. Could you provide an example of how to run code after it finishes?

 

 

 

0 Likes
Message 12 of 13

Anonymous
Not applicable

I got it solved!

Using the EventWatcher and checking all the boxes, I noticed that a transaction object was the last thing to be processed when entering a sketch. Hooking into kAfter allowed me to slice as needed. Here's the snippet and I'll post the addin once I get it tidied up a bit.

TransactionObject.PNG

 

0 Likes
Message 13 of 13

Anonymous
Not applicable
Accepted solution

Finalized code is attached. The forum won't let me post the .dll or the .addin file in order to make it drag and drop. If anyone is interested send a message!

 

The code is set up to Slice Graphics when a sketch is created or edited. Perhaps this will help somebody out.

 

Thanks @bradeneuropeArthur for stating that the sketch is not ready, as it encouraged thinking in other directions.

 

0 Likes