Edit Sketch, Delete Constraints

Edit Sketch, Delete Constraints

shastu
Advisor Advisor
2,839 Views
17 Replies
Message 1 of 18

Edit Sketch, Delete Constraints

shastu
Advisor
Advisor

When editing a Sketch, if you right mouse click on the sketch you are editing, there is an option called Delete Constraints.  Is this supported in the API for using VBA?  See attachment.

0 Likes
Accepted solutions (2)
2,840 Views
17 Replies
Replies (17)
Message 2 of 18

JelteDeJong
Mentor
Mentor

Some how i dont have this option but therefor i dont know if DimensionConstraint and/or  GeometricConstraint. Any wy here a rule that deletes both when you select a sketch.

Dim Sketch As PlanarSketch = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPlanarEntities,
                                "Select a sketch")
For Each Constraint As DimensionConstraint In Sketch.DimensionConstraints
    Constraint.Delete()
Next
For Each Constraint As GeometricConstraint In Sketch.GeometricConstraints
    If (Constraint.Deletable) Then
        Constraint.Delete()
    End If
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

Message 3 of 18

shastu
Advisor
Advisor

Are you sure that you had the sketch active?  You have to edit the sketch and then Right Mouse Click on it.  Anyway, your code is not in VBA which is what I asked for.  Can you help me do the same thing in VBA?  I might be able to figure it out myself, but you would probably be faster.

0 Likes
Message 4 of 18

WCrihfield
Mentor
Mentor

This iLogic code lets you select individual sketch entities to delete its constraints, then loops for your next selection untill you click No.

 

Repeat :
Dim oSC As SketchEntity = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveFilter, "Select a Sketch Curve.") oConsts = oSC.Constraints For Each oGC As GeometricConstraint In oConsts oGC.Delete Next For Each oDC As DimensionConstraint In oConsts oDC.Delete Next oAns = MessageBox.Show("Keep going?","LOOP",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1) If oAns = vbYes Then GoTo Repeat Else Return End If

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 18

shastu
Advisor
Advisor

Thanks, but I need the code to be VBA.

0 Likes
Message 6 of 18

JelteDeJong
Mentor
Mentor

try this:

Public Sub deleteConstraint()
    Dim Sketch As PlanarSketch
    Set Sketch = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPlanarEntities, "Select a sketch")
    
    Dim Constraint As GeometricConstraint
    For Each Constraint In Sketch.GeometricConstraints
        If (Constraint.Deletable) Then
            If (Constraint.Type <> ObjectTypeEnum.kCoincidentConstraintObject) Then
                Constraint.delete
            End If
        End If
    Next
End Sub

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

Message 7 of 18

shastu
Advisor
Advisor

I am not sure why, but after I select the sketch, it never enters the If statements.  So it goes directly from the For Each line to the End Sub.

0 Likes
Message 8 of 18

shastu
Advisor
Advisor

I do know why.  When I did a sketch.GeometricConstraints.Count it returned a value of 0.  Also, I don't think the Delete Constraints from the sketch has anything to do with geometric constraints.

0 Likes
Message 9 of 18

JelteDeJong
Mentor
Mentor

very strange for me this works. Can u send some screeen shots of what you are doing. (Or part file if you are working with inventor 2018 or earlyer)

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 10 of 18

shastu
Advisor
Advisor

This is indeed strange.  My files are already in 2019 so I created a brand new file in 2018 and tried the code on just a basic square rectangular part with one sketch and one extrusion and it worked fine.  So then I tried to reproduce the same scenario as what I had in 2019 and then the code doesn't work but if I do the command in Inventor it still does work.  I don't know yet exactly what the problem is but it could have something to do with the fact that it involves a derived part.  I wouldn't think so, but that is the one obvious difference.  I have included both files for you to investigate.  If you open the 2018BC.ipt and run the code and then select Sketch2 under Hole1 then the code never enters inside the For Each loop.

0 Likes
Message 11 of 18

Curtis_Waguespack
Consultant
Consultant

Hi @shastu 

 

This worked to delete the constraints on the file called 2018DC.ipt.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub Main()

    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oDoc.ComponentDefinition
    
    Dim oHole As HoleFeature
    Dim oSketch As Sketch
    For Each oHole In oCompDef.Features.HoleFeatures
        If Not oHole.Sketch Is Nothing Then
            Set oSketch = oCompDef.Sketches.Item(oHole.Sketch.Name)
            oSketch.Edit
            
        Dim oConstraint As DimensionConstraint
        For Each oConstraint In oSketch.DimensionConstraints
            oConstraint.Delete
        Next
        
        Dim oGeometricConstraint As GeometricConstraint
        For Each oGeometricConstraint In oSketch.GeometricConstraints
            If oGeometricConstraint.Deletable Then
                oGeometricConstraint.Delete
            End If
        Next
        
        oDoc.Update
            
        End If
    Next

End Sub

EESignature

Message 12 of 18

shastu
Advisor
Advisor

I don't want to delete the DimensionConstraints and when I comment out that section I still do not get the same results as if I actually do the command in Inventor.  I still have "pink" lines.

0 Likes
Message 13 of 18

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @shastu ,

 

Okay, give this one a try, I think this is Delete Constraints context command you mentioned in the first post.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Sub Main()

    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oDoc.ComponentDefinition
    
    
    
    Dim oHole As HoleFeature
    Dim oSketch As Sketch
    For Each oHole In oCompDef.Features.HoleFeatures
        If Not oHole.Sketch Is Nothing Then
            Set oSketch = oCompDef.Sketches.Item(oHole.Sketch.Name)
            Dim oSelSet As SelectSet
            Set oSelSet = oDoc.SelectSet
            oSketch.Edit
            Call oSelSet.Select(oSketch)

                
            ' Get the CommandManager object.
            Dim oCommandMgr As CommandManager
            Set oCommandMgr = ThisApplication.CommandManager
        
            ' Get control definition for the line command.
            Dim oControlDef As ControlDefinition
            Set oControlDef = oCommandMgr.ControlDefinitions.Item( _
                                                         "SketchDeleteConstraintsCtxCmd")
                                                         
            ' Execute the command.
            Call oControlDef.Execute

        
        oDoc.Update
            
        End If
    Next

End Sub

EESignature

Message 14 of 18

shastu
Advisor
Advisor

Thank you so much.  How did you find it?

Message 15 of 18

Curtis_Waguespack
Consultant
Consultant

@shastu wrote:

Thank you so much.  How did you find it?


I ran this (from the iLogic editor) and then searched for "DeleteConstraint" in the resulting text file... the key part to using it though is that we have to select the sketch first (since this one is a context command) .

 

' Get the CommandManager object. 
Dim oCommandMgr As CommandManager 
oCommandMgr = ThisApplication.CommandManager 

' Get the collection of control definitions. 
Dim oControlDefs As ControlDefinitions 
oControlDefs = oCommandMgr.ControlDefinitions 

'____Create and write to a text file_________________
oFile = "C:\temp\CommandNames.txt"
oWrite = System.IO.File.CreateText("C:\temp\CommandNames.txt")
oWrite.WriteLine(vbTab & "Command Name" &  vbTab &  " | " & "Description")

' Iterate through the controls and write out the name. 
For Each oControlDef In oControlDefs 
	oWrite.WriteLine(oControlDef.InternalName  & vbTab & " | " & oControlDef.DescriptionText )

Next 

oWrite.Close()
'open the file
ThisDoc.Launch(oFile)

 

EESignature

0 Likes
Message 16 of 18

JelteDeJong
Mentor
Mentor
Accepted solution

i think i found the problem. In my original post i only tested with a square that i had drawn but in your example you have a square that is a refrence to a model geometry.  It seem that the command "delete all constraints" also deletes the refrences to the model geometry. When i incorporate that in the code it seems to work. Try this code.

Public Sub deleteConstraint()
    Dim Sketch As PlanarSketch
    Set Sketch = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAllPlanarEntities, "Select a sketch")
    
    Dim line As SketchLine
    For Each line In Sketch.SketchLines
        line.Reference = False
    Next
        
    Dim Constraint As GeometricConstraint
    For Each Constraint In Sketch.GeometricConstraints
        If (Constraint.Deletable) Then
            If (Constraint.Type <> ObjectTypeEnum.kCoincidentConstraintObject) Then
                Constraint.Delete
            End If
        End If
    Next
End Sub

 Edit:

i see now that @Curtis_Waguespack has found a other solution.

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

Message 17 of 18

shastu
Advisor
Advisor

That is even BETTER!!!  I really didn't want to delete all geometric constraints but just the reference lines so this is great.  Thanks again.

0 Likes
Message 18 of 18

Jayportland
Explorer
Explorer

I know its been a while since this was asked but I just stumbled upon this thread while searching for a way to automate the deletion of coincident constraints.

The image that the original poster attached is standard functionality of Inventor.  The option in the right-click menu is a default when you have a sketch active, the caveat is that you must already have a Geometric Constraint (other than coincident) already existing in the sketch.

0 Likes