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
Solved! Go to Solution.
Solved by Sergio.D.Suárez. Go to Solution.
Solved by Sergio.D.Suárez. Go to Solution.
Solved by Sergio.D.Suárez. Go to Solution.
Solved by Sergio.D.Suárez. Go to 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"
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
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:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
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: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 !
That was close!
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
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: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 !
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
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
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
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)
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
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
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
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
Can't find what you're looking for? Ask the community or share your knowledge.