Repeat procedure for Sketches 1 To 7

Repeat procedure for Sketches 1 To 7

TobFischer94
Enthusiast Enthusiast
1,016 Views
10 Replies
Message 1 of 11

Repeat procedure for Sketches 1 To 7

TobFischer94
Enthusiast
Enthusiast

Hello,

 

i got a procedure that uses the command "auto dimension and constraints" for my sketch geometry and then presses the apply button.

 

Dim oCommandMgr As CommandManager
    oCommandMgr = ThisApplication.CommandManager
        
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item( _ 
                                                 "SketchAutoDimensionCmd")
Dim oKCmd As ControlDefinition
oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")
Call oControlDef.Execute Call oKCmd.Execute

But for some reason when i try to use his procedure on more of my Sketches "Sketch1", "Sketch2", ..., ..., "Sketch7" using a for...next Loop it only applies them to Sketch 7.

Im doing something wrong since i only use little bits of code i found online.

I tried this: https://forums.autodesk.com/t5/inventor-customization/select-and-activate-sketch-via-ilogic/td-p/528... but it doesnt work in my case. My problem seems to be activating and editing Sketch by Sketch.

 

Any ideas? Help would be very appreciated.

 

 

 

0 Likes
Accepted solutions (1)
1,017 Views
10 Replies
Replies (10)
Message 2 of 11

Sergio.D.Suárez
Mentor
Mentor

I think you've discovered an bug, is it possible @JaneFan ?
I have tried some alternative paths and generates the error that closes the program when it enters a loop, it only seems to continue when an exit for is applied to escape the loop. The following routines should work, I guess, but all end in error

 

Dim doc As PartDocument
doc = ThisDoc.Document

Dim oSketch As PlanarSketch

Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
        
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item("SketchAutoDimensionCmd")

Dim oKCmd As ControlDefinition
oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")
Dim oEditsketch As ControlDefinition
oEditsketch = ThisApplication.CommandManager.ControlDefinitions.Item("SketchEditSketchCtxCmd")
Dim oFinishSketch As ControlDefinition
oFinishSketch = ThisApplication.CommandManager.ControlDefinitions.Item("FinishSketch")

For Each oSketch In doc.ComponentDefinition.Sketches
	
oCommandMgr.DoSelect(oSketch)
oEditsketch.Execute
oControlDef.Execute
oKCmd.Execute2(True)
oFinishSketch.Execute

Next

 

Dim doc As PartDocument
doc = ThisDoc.Document

Dim oSketch As PlanarSketch

Dim ocount As Integer = doc.ComponentDefinition.Sketches.Count

Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
        
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item("SketchAutoDimensionCmd")

Dim oKCmd As ControlDefinition
oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")

Dim i As Integer
For i = 1 To ocount
	osk= doc.ComponentDefinition.Sketches.Item(i)
	osk.edit
	oControlDef.Execute
	oKCmd.Execute2(True)
	osk.exitedit
	
Next

 

Dim doc As PartDocument
doc = ThisDoc.Document

Dim oSketch As Sketch

Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
        
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item("SketchAutoDimensionCmd")

Dim oKCmd As ControlDefinition
oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")

For Each oSketch In doc.ComponentDefinition.Sketches
oSketch.edit
oControlDef.Execute
oKCmd.Execute2(True)
oSketch.exitedit

Next

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 11

TobFischer94
Enthusiast
Enthusiast

/Bump

0 Likes
Message 4 of 11

TobFischer94
Enthusiast
Enthusiast

/Bump

0 Likes
Message 5 of 11

Sergio.D.Suárez
Mentor
Mentor

Hi, since no one answers I will try to see an alternative path


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 6 of 11

Sergio.D.Suárez
Mentor
Mentor

This code was working for me, it's not exactly what you're looking for but it works fine, I'll keep testing until I can find something that works better

 

Dim oDoc As PartDocument
oDoc = ThisDoc.Document
Line1 :

Dim oCommandMgr As CommandManager
oCommandMgr = ThisApplication.CommandManager
        
Dim oControlDef As ControlDefinition
oControlDef = oCommandMgr.ControlDefinitions.Item("SketchAutoDimensionCmd")

Dim oKCmd As ControlDefinition
oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")

Dim oApply As ControlDefinition
oApply = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_ApplyCmd")


Dim oDone As ControlDefinition
oDone = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_DoneCmd")

Dim oCancel As ControlDefinition
oCancel = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_CancelCmd")

Dim oSketch As PlanarSketch
For Each oSketch In oDoc.ComponentDefinition.Sketches
	If oSketch.ConstraintStatus <> 51713 Then
	oSketch.edit
	oControlDef.Execute
	oApply.Execute2(True)
	oCancel.Execute2(True)
	oSketch.exitedit
	oCancel.Execute2(True)
	Exit For
	End If
Next

question = MessageBox.Show("Repeat again", "Dimension Sketch",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
If question = vbYes Then GoTo Line1 :

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 7 of 11

TobFischer94
Enthusiast
Enthusiast

it works for me too. Maybe we can go around the bug by also automating the pressing of the "Yes" button on the messagebox you implemented for the same amount of times as quantities of sketches exist? And Then automate the pressing of the "No" button to make the messagebox go away...

0 Likes
Message 8 of 11

Sergio.D.Suárez
Mentor
Mentor

Hi @YuhanZhang , could you check this thread? I am not an expert or anything similar in this forum, I have learned only from being self-taught, and in this thread I have found problems, that surely you have an accurate answer. Sorry for the inconvenience to call you from here. regards 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 9 of 11

YuhanZhang
Autodesk
Autodesk
Accepted solution

Hi Sergio,

 

I just created a VBA sample to do the auto-dim for sketches, you can try it there after opening your part:

 

Sub AutoDimSk()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oCommandMgr As CommandManager
    Set oCommandMgr = ThisApplication.CommandManager
        
    Dim oControlDef As ControlDefinition
    Set oControlDef = oCommandMgr.ControlDefinitions.Item( _
                                                     "SketchAutoDimensionCmd")
    Dim oKCmd As ControlDefinition
    Set oKCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_OKCmd")
    
    Dim oSk As PlanarSketch
    For Each oSk In oDoc.ComponentDefinition.Sketches
        oSk.Edit
        Call oControlDef.Execute
        Call oKCmd.Execute
        ThisApplication.UserInterfaceManager.DoEvents
        oSk.ExitEdit
    Next
    
    ThisApplication.CommandManager.StopActiveCommand
End Sub

I think you can convert it to your addin also. Please let me if it works well there.



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

TobFischer94
Enthusiast
Enthusiast

works perfectly.

Thank you very much!

0 Likes
Message 11 of 11

Sergio.D.Suárez
Mentor
Mentor

It works excellent, you are a genius @YuhanZhang , thank you for bothering to answer us.  I send you a warm greeting


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn