Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

iLogic to place point and mark for etching

andrewb
Contributor

iLogic to place point and mark for etching

andrewb
Contributor
Contributor

We've recently decided to etch all of our part numbers onto our sheet metal parts for easier identification on the shop floor, and I'm trying to make a rule to get the process somewhat automated so it's less of a slog to get through the thousands of parts we're going to have to go back and add the mark to.

I've cobbled something together from other examples of similar instances got it mostly to where I want it, but I'm hung up on some finer points.
Ideally, I'd like the rule to prompt the user to select a face (as in line 13-16), create a new sketch, place a point inside of the new sketch (location doesn't matter, as it will be moved to a more precise location later), and carry on with the rest of the rule so that all we have to do is run the rule and reposition the mark. 
It would also be great if the mark command would select all of the geometry and finalize itself, but none of what I've been able to find has worked out. 
Currently, I've just been manually placing a mark on the target face and running the rule below to get the mark in place.

Any help in the matter would be greatly appreciated.

' Creates a mark from a textbox for engraving part numbers

    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 to dimension for the new sketch
    Dim oFrontFace As Face
    oFrontFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On.")
	
    ' 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 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")

    ' Set Font Size
    Dim oTextSize As Decimal
    oTextSize = 0.47752

    ' 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 & "</StyleOverride>"

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

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

 

0 Likes
Reply
260 Views
6 Replies
Replies (6)

WCrihfield
Mentor
Mentor

To get the initial SketchPoint into your new PlanarSketch, you could use the PlanarSketch.AddByProjectingEntity method, and use the model's origin WorkPoint as the 'input' it is asking for.  The result will be a projected SketchPoint (so it will essentially be Grounded).  I would put this code between where you are initially creating the sketch, and where you are asking the user to 'Pick' an already existing SketchPoint (or instead of that code).

Oops...I guess you want the point to be movable, so a projected point will not work.  So, use the 'transient' point you are creating as the input for the PlanarSketch.SketchPoints.Add() method, to get the new SketchPoint.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

andrewb
Contributor
Contributor

That got my point placement to be automatic, which is a big step. I was apparently taking the wrong route somewhere trying to create the point. 
Running it though, it's looking I might have to make two separate rules to get the text to be tied to the point placement. The point itself is free moving, but the text doesn't move with it as it's in the same sketch. Tying in a command to run the rule to create the point is simple enough though. 
Unless there's some trick to separating two sketches in the same rule that I'm unaware of?

0 Likes

WCrihfield
Mentor
Mentor

I know you said that you need the point to be movable, so that you can put it where you want it, but then you also need to be able to 'lock it down' so that it does not move, right.  Well that can be done with sketch constraints.  There are two main 'categories' of sketch constraints (GeometricConstraints & DimensionConstraints), and each has its own set of more specific sub types.  We can create both kinds by code, and in the same rule too, if needed.  But in order to constrain the point, you may need to reference the edges of the face, or something like that.  However, if you are placing a TextBox into the sketch, and primarily just need to constrain that text in place, then you will likely need to place constraints to the geometry of the TextBox itself, instead of just a SketchPoint.  There is a tricky way to do that which I can explain further if needed.

TextBox.ShowBoundaries (setting this to True will cause the TextBox.OriginSketchPoint property to return Nothing)

TextBox.BoundaryGeometry (gets the collection of geometry representing the boundary of the TextBox, for constraining)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

andrewb
Contributor
Contributor

I had an idea and looked back to a previous iteration that I abandoned that had the text box left whole instead of converting the text to geometry. 
I added in a few things from what I have currently, hoping it would click into place better. I'm assuming this is along the lines of what you mentioned about the text box?
If so, this might be the way to go if I can get the text box to constrain to the point nicely.
*The "Mark Point" external rule is what I had to set the initial point to anchor the text to

 

 

iLogicVb.RunExternalRule("C:\$WorkingFolder\***\***_Templates\Mark Point.iLogicVb")

    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 to place text on
    Dim oFrontFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On")

    ' 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 Point to Place Text On.")

    ' Creating Custom IProperty with part number
    iProperties.Value("Custom", "PNMark") = ThisDoc.FileName(False)
  
    ' Creating a UserParameter Where the PNMark value Is put In
    Dim PropValue As String = iProperties.Value("Custom", "PNMark")

    ' Updates the user-defined Text Parameter
    Dim TagName As String = PropValue

    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)

    Dim oSketchObjects As ObjectCollection
    oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection

    ' Get all entities in the sketch
    Dim oSketchText As TextBox
    For Each oSketchText In oSketch.TextBoxes
        oSketchObjects.Add(oSketchText)
	
	' Set Font Size
    Dim oTextSize As Decimal
    oTextSize = 0.47752
	
	' Apply formatted text with the specified font size
    oTextBox.FormattedText = "<StyleOverride FontSize='" & oTextSize & "cm'>" & sText & "</StyleOverride>"	
    Next

    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)

 

 

 

billLAZKN
Explorer
Explorer

Is it OK to use your code as I have a similar issue but need the part number and project to be marked so we can laser etch it on the flat pattern.

 

Thanks, Bill

0 Likes

andrewb
Contributor
Contributor

That's exactly what we're using it for.
I've done some minor editing to it since the post. Updated code should be below. It won't be constrained to any point, but the text sketch should have a point where you can lock it in place if you'd like. Depending on the face you choose, it also sometimes places the sketch off of the surface and will throw an error, but moving the sketch to the surface and using the mark command will remedy that

    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 to place text on
    Dim oFrontFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select Surface to Place Text On")

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

    ' Creating Custom IProperty with part number
    iProperties.Value("Custom", "PNMark") = ThisDoc.FileName(False)
  
    ' Creating a UserParameter Where the PNMark value Is put In
    Dim PropValue As String = iProperties.Value("Custom", "PNMark")

    ' Updates the user-defined Text Parameter
    Dim TagName As String = PropValue

    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)

    Dim oSketchObjects As ObjectCollection
    oSketchObjects = ThisApplication.TransientObjects.CreateObjectCollection

    ' Get all entities in the sketch
    Dim oSketchText As TextBox
    For Each oSketchText In oSketch.TextBoxes
        oSketchObjects.Add(oSketchText)
	
	' Set Font Size
    Dim oTextSize As Decimal
    oTextSize = 0.47752
	
	' Apply formatted text with the specified font size
    oTextBox.FormattedText = "<StyleOverride FontSize='" & oTextSize & "cm'>" & sText & "</StyleOverride>"	
    Next

    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)

 

0 Likes