iLogic to Create Sketch

iLogic to Create Sketch

felix.cortes5K3Y2
Advocate Advocate
6,624 Views
11 Replies
Message 1 of 12

iLogic to Create Sketch

felix.cortes5K3Y2
Advocate
Advocate

Hi Forum,

 

I am trying to write a code to create a sketch. Let's say you created a new part and a sketch doesn't exist. How can I write the code so that a new sketch, "Sketch1," gets created on a plane, "YZ Plane"? Is there a way to create a text box using iLogic?

 

Best regards,

Felix Cortes

0 Likes
Accepted solutions (4)
6,625 Views
11 Replies
Replies (11)
Message 2 of 12

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi, try this code, I think this segment could serve you, greetings!

 

Dim oDoc As PartDocument = ThisDoc.Document
' Set a reference to the component definition.
Dim oCD As PartComponentDefinition
oCD = oDoc.ComponentDefinition
'Select Plane
'Dim oPlaneYZ As WorkPlane = oCD.WorkPlanes.Item(1) 
'Dim oPlaneXZ As WorkPlane = oCD.WorkPlanes.Item(2) 
Dim oPlaneXY As WorkPlane = oCD.WorkPlanes.Item(3) 
' Create a new sketch.
Dim oSketch As Sketch
oSketch = oCD.Sketches.Add(oPlaneXY)
oSketch.Name = "My_Sketch"

 


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 12

bradeneuropeArthur
Mentor
Mentor
Public Sub Main()
    ' Get the active part document.
    Dim partDoc As PartDocument
   partDoc = ThisApplication.ActiveDocument
    Dim partDef As PartComponentDefinition
   partDef = partDoc.ComponentDefinition
    
    ' Create a new sketch.
    Dim sketch As PlanarSketch
   sketch = partDef.Sketches.Add(partDef.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry
   tg = ThisApplication.TransientGeometry
    
    
    ThisApplication.ActiveView.Fit
End Sub

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

Message 4 of 12

bradeneuropeArthur
Mentor
Mentor

@Sergio.D.Suárez 

That was close!

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 5 of 12

felix.cortes5K3Y2
Advocate
Advocate

Hey Sergio,

 

The code works great on parts, however, when I try it on the flat pattern of a sheet metal part, it doesn't run it. How would the code change for the flat pattern? Also, is there a way to create a Text onto the newly created sketch using iLogic only?

 

Best regards,

Felix Cortes

0 Likes
Message 6 of 12

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi, if what you are looking for is to create a sketch in the flatt pattern, I think that a simple ilogic code like the following could be useful.

 

 

Dim oPartDoc As PartDocument = ThisDoc.Document

'verify document type is sheet metal
If oPartDoc.ComponentDefinition.Type <> 150995200 Then
	MessageBox.Show("File is not a sheet metal part.", "iLogic")
	Exit Sub
End If

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

' Check to make sure a flat pattern is open.
If Not TypeOf ThisApplication.ActiveEditObject Is FlatPattern Then
	Try
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		'Else
			'oCompDef.FlatPattern.Edit
		End If
	Catch
		MessageBox.Show("Error editting the flat pattern.", "iLogic")
	End Try
End If

'  a reference to the active flat pattern.
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern

Dim oFace As Face = oFlatPattern.TopFace ' = oFlatPattern.BottomFace

Dim oSketch As PlanarSketch
oSketch = oFlatPattern.Sketches.Add(oFace)
oSketch.Name = "My_Sketch"

 I hope the code is useful for you. 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 7 of 12

felix.cortes5K3Y2
Advocate
Advocate

Awesome! 

Message 8 of 12

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Excuse me, I just saw that you wanted to add a text, I hope the final lines can be used to give you an idea, maybe by modifying them for a later use. Regards!

'  a reference to the active document.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

'verify document type is sheet metal
If oPartDoc.ComponentDefinition.Type <> 150995200 Then
	MessageBox.Show("File is not a sheet metal part.", "iLogic")
	Exit Sub
End If

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

' Check to make sure a flat pattern is open.
If Not TypeOf ThisApplication.ActiveEditObject Is FlatPattern Then
	Try
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		'Else
			'oCompDef.FlatPattern.Edit
		End If
	Catch
		MessageBox.Show("Error editting the flat pattern.", "iLogic")
	End Try
End If

'  a reference to the active flat pattern.
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern' ThisApplication.ActiveEditObject

Dim oFace As Face = oFlatPattern.TopFace ' = oFlatPattern.BottomFace

Dim oSketch As PlanarSketch
oSketch = oFlatPattern.Sketches.Add(oFace)', False)
oSketch.Name = "My_Sketch"

'--------------------------------------------------
Dim sText As String = "Here is the last and final line of text."

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
oTextBox  = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(oSketch.OriginPointGeometry.X, _
                                oSketch.OriginPointGeometry.Y), sText)

 


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

0 Likes
Message 9 of 12

felix.cortes5K3Y2
Advocate
Advocate

Thanks Sergio!

Message 10 of 12

felix.cortes5K3Y2
Advocate
Advocate

Hi Sergio,

 

I am attempting to rotate the text to 90 degrees. I've tried the following code but it doesn't seem to work.

 

'  a reference to the active document.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

'verify document type is sheet metal
If oPartDoc.ComponentDefinition.Type <> 150995200 Then
	MessageBox.Show("File is not a sheet metal part.", "iLogic")
	Exit Sub
End If

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

' Check to make sure a flat pattern is open.
If Not TypeOf ThisApplication.ActiveEditObject Is FlatPattern Then
	Try
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		'Else
			'oCompDef.FlatPattern.Edit
		End If
	Catch
		MessageBox.Show("Error editting the flat pattern.", "iLogic")
	End Try
End If

'  a reference to the active flat pattern.
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern' ThisApplication.ActiveEditObject

Dim oFace As Face = oFlatPattern.TopFace ' = oFlatPattern.BottomFace

Dim oSketch As PlanarSketch
oSketch = oFlatPattern.Sketches.Add(oFace)', False)
oSketch.Name = "My_Sketch"

'--------------------------------------------------
Dim sText As String = "Here is the last and final line of text."

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

'oTextBox  = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(oSketch.OriginPointGeometry.X, _
'                                oSketch.OriginPointGeometry.Y), sText)

oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(oSketch.OriginPointGeometry.X, _
                                oSketch.OriginPointGeometry.Y), oTG.CreatePoint2d(0,2), sText)
'oTextBox  = oSketch.TextBoxes.AddFitted

'oTextBox  = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(oSketch.OriginPointGeometry.Y, _
'                                oSketch.OriginPointGeometry.X), sText)

oTextBox.RotateSketchObjects(oSketch,oTG.CreatePoint2d(oSketch.OriginPointGeometry.X, oSketch.OriginPointGeometry.Y), 90)

Best regards,

Felix Cortes

0 Likes
Message 11 of 12

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi Felix !, Try this code, I hope you can be useful! I found the access in the API. Best regards!

 

'  a reference to the active document.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

'verify document type is sheet metal
If oPartDoc.ComponentDefinition.Type <> 150995200 Then
	MessageBox.Show("File is not a sheet metal part.", "iLogic")
	Exit Sub
End If

Dim oCompDef As SheetMetalComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

' Check to make sure a flat pattern is open.
If Not TypeOf ThisApplication.ActiveEditObject Is FlatPattern Then
	Try
		If oCompDef.HasFlatPattern = False Then
			oCompDef.Unfold
		'Else
			'oCompDef.FlatPattern.Edit
		End If
	Catch
		MessageBox.Show("Error editting the flat pattern.", "iLogic")
	End Try
End If

'  a reference to the active flat pattern.
Dim oFlatPattern As FlatPattern = oCompDef.FlatPattern' ThisApplication.ActiveEditObject

Dim oFace As Face = oFlatPattern.TopFace ' = oFlatPattern.BottomFace

Dim oSketch As PlanarSketch
oSketch = oFlatPattern.Sketches.Add(oFace)', False)
oSketch.Name = "My_Sketch"

'--------------------------------------------------
Dim sText As String = "Here is the last and final line of text."

Dim oTG As TransientGeometry = ThisApplication.TransientGeometry

oTextBox = oSketch.TextBoxes.AddByRectangle(oTG.CreatePoint2d(oSketch.OriginPointGeometry.X, _
                                oSketch.OriginPointGeometry.Y), oTG.CreatePoint2d(0,2), sText)

  ' supports only 0, pi/2, pi, 1.5pi
 oTextBox.Rotation = PI/2     'Math.Atan(1) * 2

 


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 12 of 12

felix.cortes5K3Y2
Advocate
Advocate

I was close! Thanks Sergio