Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Cancel New Sketch event VB.NET

frederic.vandenplas
Collaborator

Cancel New Sketch event VB.NET

frederic.vandenplas
Collaborator
Collaborator

Hi,

 

In a certain condition, if a user creates a new sketch, i want to intercept that command, and abort if the conditions are met.

 

I'm now at the point where i can intercept the NewSketchEvent, but how do i cancel the event so no new sketch is created?

 

I already tried to undo or delete the last sketch, but Inventor crashes (problaby bad coding :face_with_tongue: )

 

And in the documentation, i found this...

 

SketchEvents.OnNewSketch Event

HandlingCode Output HandlingCodeEnum that indicates how you are handling the event. The value of this argument is currently ignored for this event

 

This is what i have now, i know that i'm pretty close but i have never worked with events, and there is not much information available

 

'Declare the sketchevents variable
Private WithEvents SketchEvents As SketchEvents


'Add the handler for the event

SketchEvents = InvApp.SketchEvents
                    AddHandler SketchEvents.OnNewSketch, AddressOf Me.SketchEvents.OnNewSketch

'Intercepting the event
Private Sub SketchEvents_OnNewSketch(oDoc As _Document, Sketch As Sketch, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles SketchEvents.OnNewSketch

            If BeforeOrAfter = EventTimingEnum.kBefore Then
                Dim oDocDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition

                If oDocDef.ASideDefinitions.Count = 0 Then
                    If oDoc.EnvironmentManager.EditObjectEnvironment.DisplayName = "Flat Pattern" Then
                        MessageBox.Show("You must create an A Side Definition First", "Missing A Side Definition", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    End If
                End If
            End If
        End Sub
Private m_oClosingDoc As Object 
Private Sub oAppEvents_OnClose(ByVal DocumentObject As Document, ..., ByVal BeforeOrAfter As EventTimingEnum, ...)
 If BeforeOrAfter = kBefore Then ' Check to see if the Document being closed is an interesting one. 
If Interesting(DocumentObject) Then ' Save a reference to the object.
 Set m_oClosingDoc = DocumentObject 
End If 
Else ' Now we're either after the close or it's been aborted. Check to see if the document is the interesting one.
 If DocumentObject Is m_oClosingDoc Then
 If BeforeOrAfter = kAfter then ' The document was closed.
 ElseIf BeforeOrAfter = kAbort Then ' The close was aborted. 
End If 
End If
 End Sub

 

 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Reply
460 Views
3 Replies
Replies (3)

wayne.brill
Collaborator
Collaborator

Hi Frederic,

 

I believe that deleting the sketch from the sketch OnNewSketch event is problematic. I am able to recreate the crash. I also tried using a Document Changed event to delete the sketch and Inventor crashes.

 

How important is it that the sketch is not created until the requirement is meant? Would just calling ExitEdit on the sketch in the OnNewSketch event be enough? You could display the message box telling them to add the side definition and then call ExitEdit. They would need to click on the sketch that was created to edit it. (hopefully after doing what they are supposed to).

 

Here is the VBA code I used to test ExitEdit.

 

Private Sub SketchEvents_OnNewSketch(ByVal DocumentObject As Document, ByVal Sketch As Sketch, ByVal BeforeOrAfter As EventTimingEnum, ByVal Context As NameValueMap, HandlingCode As HandlingCodeEnum)

    
    
  If BeforeOrAfter = EventTimingEnum.kAfter Then

     Dim oDocDef As SheetMetalComponentDefinition
     Set oDocDef = DocumentObject.ComponentDefinition

     If oDocDef.ASideDefinitions.count = 0 Then
           ' If DocumentObject.EnvironmentManager.EditObjectEnvironment.DisplayName = "Flat Pattern" Then
            If DocumentObject.EnvironmentManager.EditObjectEnvironment.DisplayName = "2D Sketch" Then
                'Call MsgBox("You must create an A Side Definition First", "Missing A Side Definition", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Call MsgBox("in OnNewSketch in kAfter - closing sketch, please edit the sketch after you do something")
                Sketch.ExitEdit
            End If
     End If
         
  End If
End Sub

 

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes

frederic.vandenplas
Collaborator
Collaborator

hi @wayne.brill

 

Actually, right now, I already do this as a workaround, but that way you still leave the door open for later editing without the notification. an undoable CTRL+Z would solve my problem, but then inventor crashes...

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes

wayne.brill
Collaborator
Collaborator

Hi Frederic,

 

I am not finding anyway to delete that sketch without crashing Inventor.

Please post a request for this on the Inventor Idea station:

http://forums.autodesk.com/t5/inventor-ideastation/idb-p/v1232/tab/most-recent

 

 

You could add the Sketch.ExitEdit to the OnSketchChange. This should get the message across to the user. Each time they add a sketch entity they would need start editing the sketch again. You could have a global flag and keep track of how many times the user does this. After three or so tell them that they are going to crash Inventor if they edit the sketch again and that they better save if they intend to do it. If they do edit the sketch again delete the sketch and crash Inventor.

 

This is strange to me that users would be so insistent on not following company policies about model creation. The API is not really intended to stop users from doing things in the UI.

 

Thanks,

Wayne



Wayne Brill
Developer Technical Services
Autodesk Developer Network

0 Likes