How to programmatically cancel a ControlDefinition?

How to programmatically cancel a ControlDefinition?

nannerdw
Advocate Advocate
532 Views
5 Replies
Message 1 of 6

How to programmatically cancel a ControlDefinition?

nannerdw
Advocate
Advocate

I can't figure out how to execute a control definition more than once without user interaction.  The example code below only works when I press the escape key multiple times.

I'm using control definitions to add constraints, because I couldn't get SketchBlockDefinition.GeometricConstraints to work.

 

I'm using Inventor 2020 - Build 168

 

'This iLogic code adds horizontal and vertical constraints to the selected lines.

Option Explicit On
Imports System.Linq
Dim app As Inventor.Application = ThisApplication Dim doc As Document = ThisDoc.Document Dim selSet As SelectSet = doc.SelectSet Dim cmdMgr As CommandManager = app.CommandManager Dim ctrlDefs As ControlDefinitions = cmdMgr.ControlDefinitions Dim i As Integer = 0 For Each oSketchLine In selSet.OfType(Of SketchLine).ToList selSet.Clear selSet.Select(oSketchLine) If i Mod 2 = 0 Then ctrlDefs("SketchHorizontalConstraintCmd").Execute2(True) Else ctrlDefs("SketchVerticalConstraintCmd").Execute2(True) End If i+=1 Next

 

This example uses GeometricConstraints:

'This iLogic code is supposed to add horizontal constraints to all lines in the active sketch, 
'sketch block, or sketch block definition. It only works in a sketch.

Option Explicit On

Dim app As Inventor.Application = ThisApplication
Dim doc As Document = ThisDoc.Document
Dim selSet As SelectSet = doc.SelectSet
Dim cmdMgr As CommandManager = app.CommandManager
Dim ctrlDefs As ControlDefinitions = cmdMgr.ControlDefinitions
Dim activatedObject As Object = doc.ActivatedObject
Dim oSketchLines As SketchLines

Dim gc As GeometricConstraints
If activatedObject.Type = kSketchBlockObject Then
	Dim def As SketchBlockDefinition = activatedObject.Definition
	gc = def.GeometricConstraints
	oSketchLines =def.SketchLines
Else
	gc = activatedObject.GeometricConstraints
	oSketchLines = activatedObject.SketchLines
End If

For Each tmpLine As SketchLine In oSketchLines
	gc.AddHorizontal(tmpLine)
Next 

 

0 Likes
533 Views
5 Replies
Replies (5)
Message 2 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@nannerdw,

 

Hoping that below control definition may be useful.

 

 

ctrlDefs("AppContextual_CancelCmd").Execute2(True)

 

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 6

JelteDeJong
Mentor
Mentor

this iLogic rule will add a horizontal constaint to all sketch lines in a sketch that is activated.

Dim doc As PartDocument = ThisApplication.ActiveDocument
If (doc.SketchActive = False) Then
    MsgBox("None sketch active")
End If
Dim Sketch As PlanarSketch = doc.ActivatedObject

Dim geomConstraints As GeometricConstraints = Sketch.GeometricConstraints
For Each line As SketchLine In Sketch.SketchLines
    geomConstraints.AddHorizontal(line)
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 4 of 6

nannerdw
Advocate
Advocate

That is the same method as the second example that I already posted.  It does not work inside sketch blocks.

I'm using control definitions to add constraints, because I couldn't get "SketchBlockDefinition.GeometricConstraints" to work.

0 Likes
Message 5 of 6

nannerdw
Advocate
Advocate

Adding "AppContextual_CancelCmd" causes only the first constraint to be automatically created, then it just switches to the manual command after that.  If I instead press Esc multiple times, it continues adding all of the automatic constraints, but only if I keep manually pressing Esc repeatedly, the same as what my first example did.

0 Likes
Message 6 of 6

JelteDeJong
Mentor
Mentor

I assumed wrong that you where creating a sketch. Maby this helps:

Dim doc As PartDocument = ThisApplication.ActiveDocument
Dim blockName = "Block1"

Dim sketchBlock As SketchBlockDefinition = doc.ComponentDefinition.SketchBlockDefinitions.Item(blockName)
Dim geomConstraints As GeometricConstraints = sketchBlock.GeometricConstraints
For Each line As SketchLine In sketchBlock.SketchLines
    geomConstraints.AddHorizontal(line)
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes