CHANGE HEIGHT IN TEXT BOX (SKETCH 2D) ILOGIC

CHANGE HEIGHT IN TEXT BOX (SKETCH 2D) ILOGIC

roberto_infanti
Contributor Contributor
1,279 Views
11 Replies
Message 1 of 12

CHANGE HEIGHT IN TEXT BOX (SKETCH 2D) ILOGIC

roberto_infanti
Contributor
Contributor

I'VE CREATED A MACRO THAT INSERTS TEXT AND CREATES THE INSTRUCTION ON A SELECTED FACE. HOW DO I CHANGE THE TEXT HEIGHT FROM ILOGIC RULE?
FOR THE BEST ONES: HOW DO I CHOOSE WITH ONE CLICK THE INSERTION POINT OF THE TEXT BOX (THE GEOMETRIC CENTER OF THE FACE COULD ALSO BE FINE)

 

 

' Dichiarazione delle variabili
Dim oPart As PartDocument
Dim oFace As Face
Dim oSketch As PlanarSketch
Dim oText As TextBox
Dim oExtrudeDef As ExtrudeDefinition

' Ottieni il documento corrente
oPart = ThisApplication.ActiveDocument

' Seleziona la faccia
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Seleziona una faccia")

' Crea uno schizzo sulla faccia selezionata
oSketch = oPart.ComponentDefinition.Sketches.Add(oFace)

' Aggiungi il testo allo schizzo
oText = oSketch.TextBoxes.AddFitted(ThisApplication.TransientGeometry.CreatePoint2d(0, 0), "PROVA") 

' Crea un profilo dallo schizzo
Dim oProfile As Profile
oProfile = oSketch.Profiles.AddForSolid()

' Crea la definizione dell'estrusione
oExtrudeDef = oPart.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation)

' Imposta la distanza di estrusione

oDistance = 0.5

' Imposta l'estrusione per essere simmetrica
oExtrudeDef.SetDistanceExtent(oDistance, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)

' Crea l'estrusione
Dim oExtrude As ExtrudeFeature
oExtrude = oPart.ComponentDefinition.Features.ExtrudeFeatures.Add(oExtrudeDef)
0 Likes
Accepted solutions (2)
1,280 Views
11 Replies
Replies (11)
Message 2 of 12

roberto_infanti
Contributor
Contributor

SOLUTION WITH NEW STYLE:

' Dichiarazione delle variabili
Dim oPart As PartDocument
Dim oFace As Face
Dim oSketch As PlanarSketch
Dim oText As TextBox
Dim oExtrudeDef As ExtrudeDefinition

' Ottieni il documento corrente
oPart = ThisApplication.ActiveDocument

' Seleziona la faccia
oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Seleziona una faccia")

' Crea uno schizzo sulla faccia selezionata
oSketch = oPart.ComponentDefinition.Sketches.Add(oFace)

' Aggiungi il testo allo schizzo
oText = oSketch.TextBoxes.AddFitted(ThisApplication.TransientGeometry.CreatePoint2d(0, 0), NOME_FILE,"NOTA_STAMPI_3D_H30MM")

' Crea un profilo dallo schizzo
Dim oProfile As Profile
oProfile = oSketch.Profiles.AddForSolid()

' Crea la definizione dell'estrusione
oExtrudeDef = oPart.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation)

' Imposta la distanza di estrusione

oDistance = 0.5

' Imposta l'estrusione per essere simmetrica
oExtrudeDef.SetDistanceExtent(oDistance, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)

' Crea l'estrusione
Dim oExtrude As ExtrudeFeature
oExtrude = oPart.ComponentDefinition.Features.ExtrudeFeatures.Add(oExtrudeDef) WITH

 

0 Likes
Message 3 of 12

Michael.Navara
Advisor
Advisor

Here is modified your first example with automatic face center detection and input for text height

Sub Main()
    ' Dichiarazione delle variabili
    Dim oPart As PartDocument
    Dim oFace As Face
    Dim oSketch As PlanarSketch
    Dim oText As Inventor.TextBox
    Dim oExtrudeDef As ExtrudeDefinition

    ' Ottieni il documento corrente
    oPart = ThisApplication.ActiveDocument

    ' Seleziona la faccia
    oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Seleziona una faccia")

    ' Crea uno schizzo sulla faccia selezionata
    oSketch = oPart.ComponentDefinition.Sketches.Add(oFace)

    ' Aggiungi il testo allo schizzo
    Dim position As Point2d = GetPosition(oSketch)
    Dim height As Double = Double.Parse(InputBox("Input height [mm]", "Input text height", "30")) / 10
    Dim formattedText As String = GetFormattedText("PROVA", height)
    oText = oSketch.TextBoxes.AddFitted(position, formattedText)
    oText.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle
    oText.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter

    ' Crea un profilo dallo schizzo
    Dim oProfile As Profile
    oProfile = oSketch.Profiles.AddForSolid()

    ' Crea la definizione dell'estrusione
    oExtrudeDef = oPart.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation)

    ' Imposta la distanza di estrusione

    Dim oDistance = 0.5

    ' Imposta l'estrusione per essere simmetrica
    oExtrudeDef.SetDistanceExtent(oDistance, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)

    ' Crea l'estrusione
    Dim oExtrude As ExtrudeFeature
    oExtrude = oPart.ComponentDefinition.Features.ExtrudeFeatures.Add(oExtrudeDef)
End Sub

Private Function GetPosition(sketch As PlanarSketch) As Point2d
    'Dim position As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    Dim face As Face = CType(sketch.PlanarEntity, Face)
    If face Is Nothing Then
        Return ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    End If

    Dim paramRangeRect As Box2d = face.Evaluator.ParamRangeRect
    Dim centerParamX = (paramRangeRect.MaxPoint.X + paramRangeRect.MinPoint.X) / 2
    Dim centerParamY = (paramRangeRect.MaxPoint.Y + paramRangeRect.MinPoint.Y) / 2

    Dim points As Double() = {}
    face.Evaluator.GetPointAtParam({centerParamX, centerParamY}, points)

    Dim positionInModelSpace As Point = 
            ThisApplication.TransientGeometry.CreatePoint(points(0), points(1), points(2))
    Dim positionInSketchSpace As Point2d = sketch.ModelToSketchSpace(positionInModelSpace)
    Return positionInSketchSpace

End Function

Private Function GetFormattedText(content As String, height As Double) As String
    Return String.Format("<StyleOverride FontSize='{1}'>{0}</StyleOverride>", content, height)
End Function
Message 4 of 12

roberto_infanti
Contributor
Contributor

Good morning! Perfect!! Congratulations! Can I ask you one more thing? Let's see if I can explain. If I enter the text manually, I'm going to insert it as a "user parameter", so the file updates every time it changes. On the other hand, when I insert it through the ilogic rule, it is not a parameter but it is a common text. Is there a way to put it on the ilogic rule?
Basically, I have a .ipt file that has the writing on many faces. I use the same file as a master to create others. The inscription has the data from ipropeties. Thanks

0 Likes
Message 5 of 12

Michael.Navara
Advisor
Advisor

Everything is about FormattedText. The easiest way is to create text manually and look in to the FormattedText property of TextBox. Then you can update the code.

0 Likes
Message 6 of 12

roberto_infanti
Contributor
Contributor

Unfortunately I can't find anything....help me....Thanks

0 Likes
Message 7 of 12

Michael.Navara
Advisor
Advisor

1) Create text box in the sketch

2) Set format and content as you want

3) Look in to the API. For example using my tool VBA Object Browser for Inventor or print the FormattedText to iLogic log console.

4) Look at the value of this property

5) You get string value similar to this

"<StyleOverride FontSize='1,'><Parameter Resolved='True' ComponentIdentifier='Part1' Name='test' Precision='3'>1.000</Parameter></StyleOverride>"

Update the code according to this.

More info about FormattedText tags is here.

 

MichaelNavara_1-1700134758968.png

 

Message 8 of 12

roberto_infanti
Contributor
Contributor

YOU ARE MAKING ME DISCOVER A NEW WORLD. I'LL GIVE IT A TRY! THANK YOU

0 Likes
Message 9 of 12

roberto_infanti
Contributor
Contributor

Unfortunately I can't

0 Likes
Message 10 of 12

roberto_infanti
Contributor
Contributor
Accepted solution
Sub Main()
    Dim oApp As Inventor.Application
    Dim oPart As PartDocument
    oApp = ThisApplication

    oPart = oApp.ActiveDocument ' Corrected line

    ' Clear the current selection set
    oApp.ActiveDocument.SelectSet.Clear()

    ' Allow the user to select a face
    Dim oFace As Face
    oFace = oApp.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Seleziona una faccia")

    ' Add the selected face to the selection set
    oApp.ActiveDocument.SelectSet.Select(oFace)

    ' Now you can create a sketch on the selected face
    Dim oSketch As PlanarSketch
    oSketch = oPart.ComponentDefinition.Sketches.Add(oFace) ' Corrected line

    Dim oTG As TransientGeometry
    oTG = oApp.TransientGeometry

    Dim oText As TextBox
    Dim position As Point2d = GetPosition(oSketch)
    'Dim height As Double = Double.Parse(InputBox("Input height [mm]", "Input text height", "30")) / 10
	
    oText = oSketch.TextBoxes.AddFitted(position, "<Parameter ComponentIdentifier='" & oPart.FullFileName & "' Name='NOME_FILE' Precision='2' ></Parameter>","NOTA 30 MM") ' Corrected line

    oPart.Update2(True) ' Corrected line
	' Crea un profilo dallo schizzo
    Dim oProfile As Profile
    oProfile = oSketch.Profiles.AddForSolid()

    ' Crea la definizione dell'estrusione
    Dim oExtrudeDef As ExtrudeDefinition ' Corrected line
    oExtrudeDef = oPart.ComponentDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, PartFeatureOperationEnum.kCutOperation)

    ' Imposta la distanza di estrusione
    Dim oDistance As Double = 0.5 ' Corrected line

    ' Imposta l'estrusione per essere simmetrica
    oExtrudeDef.SetDistanceExtent(oDistance, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)

    ' Crea l'estrusione
    Dim oExtrude As ExtrudeFeature
    oExtrude = oPart.ComponentDefinition.Features.ExtrudeFeatures.Add(oExtrudeDef)
End Sub


Private Function GetPosition(sketch As PlanarSketch) As Point2d
    Dim face As Face = CType(sketch.PlanarEntity, Face)
    If face Is Nothing Then
        Return ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    End If

    Dim paramRangeRect As Box2d = face.Evaluator.ParamRangeRect
    Dim centerParamX = (paramRangeRect.MaxPoint.X + paramRangeRect.MinPoint.X) / 2
    Dim centerParamY = (paramRangeRect.MaxPoint.Y + paramRangeRect.MinPoint.Y) / 2

    Dim points As Double() = {}
    face.Evaluator.GetPointAtParam({centerParamX, centerParamY}, points)

    Dim positionInModelSpace As Point = 
            ThisApplication.TransientGeometry.CreatePoint(points(0), points(1), points(2))
    Dim positionInSketchSpace As Point2d = sketch.ModelToSketchSpace(positionInModelSpace)
    Return positionInSketchSpace
End Function
0 Likes
Message 11 of 12

projetos33
Contributor
Contributor

I'm trying to create a sketch on the unfolded part, but I'm having trouble.

I'm doing it in iLogic, and when I run the rule, it creates a sketch on the face of the unfolded view, and creates a text box with the file name.

I need this name to be in the size 6.00mm and Font: "Txt"

This rule works on metal parts with unfolding.

 

Sub Main()
    ' Obter o documento ativo
    Dim oPartDoc As PartDocument
    oPartDoc = ThisApplication.ActiveDocument
 
    ' Verificar se o documento é uma chapa metálica
    If oPartDoc.ComponentDefinition.Type <> 150995200 Then
        MessageBox.Show("O arquivo não é uma peça de chapa metálica.", "iLogic")
        Exit Sub
    End If
 
    ' Obter a definição da peça de chapa metálica
    Dim oCompDef As SheetMetalComponentDefinition
    oCompDef = oPartDoc.ComponentDefinition
 
    ' Verificar e ativar a vista planificada
    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("Erro ao ativar a vista planificada.", "iLogic")
            Exit Sub
        End Try
    End If
 
    ' Obter a vista planificada
    Dim oFlatPattern As FlatPattern
    oFlatPattern = ThisApplication.ActiveEditObject
 
    ' Obter a face do padrão planificado
    Dim oFace As Face
    oFace = oFlatPattern.TopFace
 
    ' Nome do esboço e do corte
    Dim sSketchName As String
    Dim sTextFeatureName As String
    sSketchName = "Text Marking Sketch"
    sTextFeatureName = "Text Marking"
 
    ' Remover esboço existente, se houver
    Dim oSketch As PlanarSketch
    For Each oSketch In oFlatPattern.Sketches
        If oSketch.Name = sSketchName Then
            oSketch.Delete
        End If
    Next
 
    ' Criar um novo esboço na face planificada
    oSketch = oFlatPattern.Sketches.Add(oFace, False)
    oSketch.Name = sSketchName
 
    ' Obter o nome do arquivo (sem extensão)
    Dim sFileName As String
    sFileName = System.IO.Path.GetFileNameWithoutExtension(oPartDoc.FullFileName)
 
    ' Criar um ponto de origem para o texto
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry
    Dim oTextPosition As Point2d
    oTextPosition = oTransGeom.CreatePoint2d(0, 0) ' Posição inicial (pode ser ajustada)
 
    ' Adicionar um texto ao esboço
Dim oTextBox As Inventor.TextBox
oTextBox = oSketch.TextBoxes.AddFitted(oTextPosition, sFileName)
 
    ' Mensagem de sucesso
    MsgBox ("Marcação com o nome da peça foi adicionada!", vbInformation, "Concluído")
 
End Sub

 

0 Likes
Message 12 of 12

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@projetos33 wrote:

I'm trying to create a sketch on the unfolded part, but I'm having trouble.

I'm doing it in iLogic, and when I run the rule, it creates a sketch on the face of the unfolded view, and creates a text box with the file name.

I need this name to be in the size 6.00mm and Font: "Txt"

This rule works on metal parts with unfolding.


see answer in duplicate post here:

https://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-text-box-size-and-font-flat-patter...

 

EESignature

0 Likes