iLogic to create Sketch Text > mark with specific Mark Style (Mark Surface or Mark Through), specify Text Height.

iLogic to create Sketch Text > mark with specific Mark Style (Mark Surface or Mark Through), specify Text Height.

ivan_petrovJK76C
Participant Participant
930 Views
8 Replies
Message 1 of 9

iLogic to create Sketch Text > mark with specific Mark Style (Mark Surface or Mark Through), specify Text Height.

ivan_petrovJK76C
Participant
Participant

Hi everyone

 

Extremely helpful community!

 

I am not a programmer/coder, but trying to put together an iLogic to help my and my coleagues workload. We have many parts that we need to etch on our laser. Idea is to pick a face on the part, create sketch text with iProperties"Part Number" and iProperties"Revision Number" ideally as a single text box so they can be dragged together (stay aligned). Then create Mark Through from that text, while forcing the text height to change to 6 (for example).

 

This is what I run at the moment, I have another rule that is the same just for the "Revision Number" property, which is not ideal and would like to combine it in one single rule. I cant pick text height and Mark Though with the below rule. Any help would be appreciated, thanks a lot!

 

Sub Main()

 '  a reference to the currently active document.
 ' This assumes that it is a part document.
	    Dim oPartDoc As PartDocument
	    oPartDoc = ThisApplication.ActiveDocument
	
		Dim oCompDef As PartComponentDefinition
		oCompDef = oPartDoc.ComponentDefinition

  ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry

'================ Pasted to pick the face where the Mark has to come ============================

'Select Face and Edges to dimension sketch circles from
Dim oFrontFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On")

'============================================================

		    ' Create a new sketch on this face
			    oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
			    oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
		
		    ' Determine where in sketch space the point (0.5,0.5,0) is.
			  Dim oCorner As Point2d
			    oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 0.5, 0))




		'Creating a UserParameter Where the chosen iProperties value Is put In
		
		Dim PropValue As String = iProperties.Value("Project", "Part Number")
		
		'updates the user-defined Text Parameter
		TagName = PropValue
		
'================================ Create a textbox in the Sketch to place the Iporp Tekeningnummer ====================
	 
		'     Create Text With simple String As Input.  Since this doesn't use
		'     any Text Overrides, it will Default To the active Text Style.
		    
			Dim oTG As TransientGeometry
		    oTG = ThisApplication.TransientGeometry
			
			Dim sText As String
		    sText = TagName
		    Dim oTextBox As TextBox
		    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(1, 1), sText)									

'==================== Creating an Objectcollection to be Marked ======================
    Dim oSketchObjects As ObjectCollection
    oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection
    
    ' Get all entities in the sketch
    Dim oSketchText As Inventor.TextBox
    For Each oSketchText In oSketch.TextBoxes
        oSketchObjects.Add(oSketchText)
    Next

'==================== Creating the Mark on the textbox  ======================

    Dim oMarkFeatures As MarkFeatures
    oMarkFeatures = oCompDef.Features.MarkFeatures
    
    ' Get a mark style.
    Dim oMarkStyle As MarkStyle
    oMarkStyle = oPartDoc.MarkStyles.Item(1)
    
	' Create mark definition.
	Dim oMarkDef As MarkDefinition
	oMarkDef = oMarkFeatures.CreateMarkDefinition(oSketchObjects, oMarkStyle)
	
	' Create a mark feature.
	Dim oMark As MarkFeature
	oMark = oMarkFeatures.Add(oMarkDef)

'====================   
	
End Sub
0 Likes
Accepted solutions (2)
931 Views
8 Replies
Replies (8)
Message 2 of 9

RomPoteauxGP
Explorer
Explorer
Accepted solution

Hi, I had an almost similar need.... have a look at the below and it might inspire you! 

 

Sub Main()
    'This is an iLogic rule to create a mark from a textbox for engraving a reference to the currently active document.
    'This assumes that it is a part document.
    
    Dim oPartDoc As PartDocument
    oPartDoc = ThisApplication.ActiveDocument
    
    Dim oCompDef As PartComponentDefinition
    oCompDef = oPartDoc.ComponentDefinition
    
    ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry
    
    ' Select Face and Edges to dimension sketch circles from
    Dim oFrontFace As Face
    oFrontFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On."& CHR(10)& " Ideally select front face or you can rearrange the geometries in the sketch later")
    
    ' Create a new sketch on this face
    Dim oSketch As PlanarSketch
    oSketch = oCompDef.Sketches.Add(oFrontFace)
    
    ' Determine where in sketch space the point (0.5,0.5,0) is.
    Dim oCorner As Point2d
    oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 1, 0))
    
    ' Create text with a simple string as input.
    Dim sText As String
    sText = iProperties.Value("Project", "Part Number")
    
    Dim oTextBox As TextBox
    oTextBox = oSketch.TextBoxes.AddFitted(oCorner, sText)
    oTextBox.FormattedText = "<StyleOverride FontSize='0.5'>" & sText & "</StyleOverride>"
    
    oTextBox.ConvertToGeometry("ISO")
    
    ' Start the feature command
    ThisApplication.CommandManager.ControlDefinitions.Item("PartMarkingCmd").Execute()
End Sub
Message 3 of 9

ivan_petrovJK76C
Participant
Participant

Thanks for replying @RomPoteauxGP !

 

Great help, this fixes text height and prompting when marking, so I can choose the type of mark I would like. 🙂

 

Could 2 different iProperties be added to the same String, or if not how to keep them together in the same text line/row so they can be dragged together?

 

Thanks and have a nice day!

0 Likes
Message 4 of 9

RomPoteauxGP
Explorer
Explorer
Accepted solution

yes you can, i've just tried adding the Rev number. 

 ' Create text with a simple string as input.
    Dim sText As String
    sText = iProperties.Value("Project", "Part Number")
	    Dim sText1 As String
    sText1 = iProperties.Value("Project", "Revision Number")
    
    Dim oTextBox As TextBox
    oTextBox = oSketch.TextBoxes.AddFitted(oCorner, sText)
    oTextBox.FormattedText = "<StyleOverride FontSize='0.5'>" & sText & "-Rev"& sText1 &"</StyleOverride>"
Message 5 of 9

ivan_petrovJK76C
Participant
Participant

Amazing, thanks a lot @RomPoteauxGP!

Have it all sorted now and you have big thanks from the team here as well! 🙂

Message 6 of 9

Rich-T
Advocate
Advocate

Nice rule, could I request help with 1 or 2 additions?

 

Firstly, I'd like the user to be able to enter a font size.

Secondly, it'd be great if the user could pick the insertion point after choosing the face rather than use the fixed

co-ord method.

 

0 Likes
Message 7 of 9

RomPoteauxGP
Explorer
Explorer

There you go.

You just need to ensure there is an existing sketch with a point in. 

 

Sub Main()
    ' This is an iLogic rule to create a mark from a textbox for engraving a reference to the currently active document.
    ' This assumes that it is a part document.

    Dim oPartDoc As PartDocument
    oPartDoc = ThisApplication.ActiveDocument

    Dim oCompDef As PartComponentDefinition
    oCompDef = oPartDoc.ComponentDefinition

    ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry

    ' Select Face and Edges to dimension sketch circles from
    Dim oFrontFace As Face
    oFrontFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On." & Chr(10) & " Ideally select front face or you can rearrange the geometries in the sketch later")

    ' Create a new sketch on this face
    Dim oSketch As PlanarSketch
    oSketch = oCompDef.Sketches.Add(oFrontFace)

    ' Determine where in sketch space the point is
    Dim oCornerPick As SketchPoint
    oCornerPick = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchPointFilter, "Select Top left point to Place Text On.")

    ' Convert the selected point to SketchPoint if necessary
    Dim oCorner As Point2d
    oCorner = oTransGeom.CreatePoint2d(oCornerPick.Geometry.X, oCornerPick.Geometry.Y)

    ' Get text from iProperties
    Dim sText As String
    sText = iProperties.Value("Project", "Part Number")
    Dim sText1 As String
    sText1 = iProperties.Value("Project", "Revision Number")

    ' Get Font Size from user input
    Dim oTextSize As Decimal
    oTextSize = InputBox("Font size? (in cm)")

    ' Create text box with formatted text
    Dim oTextBox As TextBox
    oTextBox = oSketch.TextBoxes.AddFitted(oCorner, sText)

    ' Apply formatted text with the specified font size
    oTextBox.FormattedText = "<StyleOverride FontSize='" & oTextSize & "cm'>" & sText & "-Rev" & sText1 & "</StyleOverride>"

    ' Convert the text box to geometry
    oTextBox.ConvertToGeometry("ISO")

    ' Start the feature command (if needed, depending on functionality)
    ThisApplication.CommandManager.ControlDefinitions.Item("PartMarkingCmd").Execute()
End Sub

 

Message 8 of 9

ivan_petrovJK76C
Participant
Participant

Hi Rich

 

I am not a coder myself, that is why I raised my issue here and got helped. Suggest you try as well, ppl are pretty knowledgeable ...

 

You need the oCorner (line 40) and FontSize at line 68 to be set as prompt entries when iLogic is ran. Hope this would at least help you formulate your question in your post easier.

 

Have a nice day!

0 Likes
Message 9 of 9

Rich-T
Advocate
Advocate

Here's some code it cobbled together using code form JelteDeJong.

It works up to a point but fails at the end.

 

Can anyone help ?

 

Sub Main()
'===================== DECLERATIONS ===============================

' Reference the currently active document.
' This assumes that it is a part document.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument

Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition

' Set a reference to the transient geometry object.
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry

Dim oPartCompDef As PartComponentDefinition
oPartCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'=========================================================================
'Find Sketch named already For marking so the user can delete it
For Each oSketchCheck As PlanarSketch In oPartCompDef.Sketches
	If oSketchCheck.Name = "MarkingSketch" Then
		MessageBox.Show("A sketch named " & oSketchCheck.Name & " already exists. Please Delete this sketch", "Control of MarkingSketch")
		Return
	End If
Next
'=========================================================================

'Select Face and Edges to place the textbox in a certain direction
Dim oFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Textbox")
Dim oHorizontalEdge As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select Horizontal Edge of Face")
Dim orignVertex As Vertex = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartVertexFilter, "Select Vertex")
Dim testVertex As Vertex = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartVertexFilter, "Select a 2nd vertex - that is on a positive point in the sketch")

'Add Sketch and set sketch origin point to the stopVertex of the FrontEdge, this is where the TextBox starts
Dim oSketch As PlanarSketch = oPartCompDef.Sketches.Add(oFace, False)
oSketch.Name = "MarkingSketch"

oSketch.AxisEntity = oHorizontalEdge
oSketch.OriginPoint = orignVertex
oSketch.NaturalAxisDirection = True

'Check if the testVertex has positive coordinates 
Dim testPoint As Point2d = oSketch.ModelToSketchSpace(testVertex.Point)
If (testPoint.X <= 0) Then
	oSketch.NaturalAxisDirection = False
End If

testPoint  = oSketch.ModelToSketchSpace(testVertex.Point)
Dim positiveQuadrantIsOnPlane = (testPoint.Y >= 0)

' ===================== Create a Custom Parameter in Iproperties Tekeningnummer ======================
'Creating Custom IProperty with filenumber in it calling it Tekeningnummer

 ' Get text from iProperties
 Dim UsrText As String
 UsrText = iProperties.Value("Project", "Part Number")
 Dim UsrText1 As String
 UsrText1 = iProperties.Value("Project", "Revision Number")

'updates the user-defined Text Parameter
Dim TagName As String = UsrText & "_" & UsrText1 
'MessageBox.Show(TagName)

'================================ Create a textbox in the Sketch to place the Iporp Tekeningnummer ====================
' Create Text With simple String As Input.  Since this doesn't use
' any Text Overrides, it will Default To the active Text Style.

Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry

Dim sText As String
sText = TagName

Dim oTextBox As Inventor.TextBox
If (positiveQuadrantIsOnPlane) Then
	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(0.25, 0), sText)
Else
	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(0.5, -0.5), sText)
End If


'Changing Font and/or size
Dim dTextSize As Double
dTextSize = 0.30 ' value is in cm.  equals .125 in
Dim sTextFont As String
'			sTextFont = "SIMPLEX"
oTextBox.FormattedText = "<StyleOverride Font = '" & sTextFont & "' FontSize = '" & dTextSize & "'>" & sText & "</StyleOverride>"

' Ensure the text box is converted to geometry correctly

    ' Convert the TextBox to geometry
    Dim geomEntities As Object = Nothing
    geomEntities = oTextBox.ConvertToGeometry("ISO")

    ' Create an ObjectCollection
    Dim oGeom As ObjectCollection = Nothing
    oGeom = ThisApplication.TransientObjects.CreateObjectCollection

    ' Add the converted geometry to the ObjectCollection
    oGeom.Add(geomEntities)


    Dim oMarkFeatures As MarkFeatures
	
	Try
    oMarkFeatures = oCompDef.Features.MarkFeatures
	   Catch ex As Exception
        MessageBox.Show("Error defining oMarkFeatures: " & ex.Message)
        Return
    End Try
    
    ' Get a mark style.
    Dim oMarkStyle As MarkStyle
   Try
	   oMarkStyle = oPartDoc.MarkStyles.Item(1)
   Catch ex As Exception
        MessageBox.Show("Error getting MarkStyle: " & ex.Message)
        Return
    End Try
	
    ' Create mark definition.
    Dim oMarkDef As MarkDefinition
        Try
          MarkDef = oMarkFeatures.CreateMarkDefinition(oGeom, oMarkStyle)
    Catch ex As Exception
        MessageBox.Show("Error adding MarkDefinition: " & ex.Message)
        Return
    End Try

    ' Create a mark feature.
    Dim oMark As MarkFeature
    Try
          oMark = oMarkFeatures.Add(oMarkDef)
    Catch ex As Exception
        MessageBox.Show("Error adding MarkFeature: " & ex.Message)
        Return
    End Try

End Sub

 

0 Likes