Need some help with iLogic rule

Need some help with iLogic rule

mgrenier2
Advocate Advocate
220 Views
3 Replies
Message 1 of 4

Need some help with iLogic rule

mgrenier2
Advocate
Advocate

I'm trying to use iLogic to create a note on a drawing sheet and apply a certain text style to it, I'm not having much success so far. Can someone take a look at this and point me to where I messed up?

' Récupération du document de dessin actif
Dim drawingDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim feuille As Sheet = drawingDoc.ActiveSheet

' Définir le texte à insérer
Dim texte As String = "Exemple de texte"
Dim positionX As Double = 10 ' Position X du texte
Dim positionY As Double = 10 ' Position Y du texte
Dim pointInsertion As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(positionX, positionY)

' Création de la note de texte
Dim noteTexte As DrawingNote = feuille.DrawingNotes.GeneralNotes.AddFitted(pointInsertion, texte)

' Nom du style de texte à appliquer
Dim styleNom As String = "Barcode" ' Remplacez par le nom de votre style
Dim styleTexte As TextStyle = Nothing

' Recherche du style dans la liste des styles de texte
Dim styleTrouve As Boolean = False
For Each styleTexte In drawingDoc.StylesManager.TextStyles
    If styleTexte.Name = styleNom Then
        ' Appliquer le style trouvé à la note de texte
        noteTexte.TextStyle = styleTexte
        styleTrouve = True
        Exit For
    End If
Next

' Si le style est trouvé, centrer et appliquer les autres propriétés
If styleTrouve Then
    noteTexte.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
    noteTexte.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle
    noteTexte.Color = ThisApplication.TransientObjects.CreateColor(0, 0, 0) ' Noir
Else
    ' Message si le style n'a pas été trouvé
    MessageBox.Show("Le style de texte spécifié n'a pas été trouvé.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
0 Likes
221 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @mgrenier2.  Are you getting an error message pop-up, or is it just not doing what you want it to do?  If it is showing an error, then please post what the 'More Info' tab of that error message says.

Dealing with 'styles' in a drawing by code can be complicated, but especially so when those styles have been overridden by further changes to the instance objects after they have been created.  Once some of those settings have been overridden, it can be more challenging to set that instance object (such as a GeneralNote) to a different specific 'style', and have that style's settings apply to that object.  Most of your code looks OK to me, but I would declare the Type of your 'noteTexte' variable as the more specific 'GeneralNote' Type instead of the more generic 'DrawingNote' Type.  That will enable/expose more of its specific properties.  Not sure if that will fix your current issue(s) though.  Also, I would move the code that 'gets' the TextStyle to above where you create the GeneralNote, then include that TextStyle as the third (and Optional) input into the GeneralNotes.AddFitted method.

 

Edit:  Also, if the TextStyle you are getting is only located in a global StylesLibrary, and there is not a 'local' copy of it in that specific document, then it might not work, because you may need to copy that style into that document first, then use that local copy of the style when setting the style of the existing GeneralNote.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

Curtis_Waguespack
Consultant
Consultant

@mgrenier2, I think you just need to use the formatted text, and then what you have will work. see example.

 

note I changed the font to Stencil rather than Barcode ( I didn't have a barcode font loaded)

 

' Récupération du document de dessin actif
Dim drawingDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim feuille As Sheet = drawingDoc.ActiveSheet

' Définir le texte à insérer
Dim texte As String = "Exemple de texte"
Dim positionX As Double = 10 ' Position X du texte
Dim positionY As Double = 10 ' Position Y du texte
Dim pointInsertion As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(positionX, positionY)

' Nom du style de texte à appliquer
Dim styleNom As String = "Stencil" ' Remplacez par le nom de votre style
Dim styleTexte As TextStyle = Nothing

' Recherche du style dans la liste des styles de texte
Dim styleTrouve As Boolean = False
For Each styleTexte In drawingDoc.StylesManager.TextStyles
    If not styleTexte.Name = styleNom Then
        ' Appliquer le style trouvé à la note de texte
        texte = "<StyleOverride Font='" & styleNom & "' FontSize = '0.5' >" & texte & "</StyleOverride>"
        styleTrouve = True
        Exit For
    End If
Next

' Création de la note de texte
Dim noteTexte As DrawingNote = feuille.DrawingNotes.GeneralNotes.AddFitted(pointInsertion, texte)

' Si le style est trouvé, centrer et appliquer les autres propriétés
If styleTrouve Then
    noteTexte.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
    noteTexte.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle
    noteTexte.Color = ThisApplication.TransientObjects.CreateColor(0, 0, 0) ' Noir
Else
    ' Message si le style n'a pas été trouvé
    MessageBox.Show("Le style de texte spécifié n'a pas été trouvé.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

 

EESignature

0 Likes
Message 4 of 4

mgrenier2
Advocate
Advocate

Here's what I ended up with. Thanks Curtis

 

' Variables pour la génération de numéros aléatoires
Dim random As New Random() ' Objet random pour générer les numéros
Dim texte As String = "" ' Texte à insérer dans le dessin
Dim numeroUtilise As Long = 0 ' Numéro utilisé dans la note, défini comme Long pour éviter les erreurs de taille

' Charger l'historique des numéros utilisés depuis Excel
Dim numeration As New List(Of Long)() ' Liste pour stocker les numéros utilisés
Dim excelApp As Object = CreateObject("Excel.Application")
Dim workbook As Object = Nothing
Dim worksheet As Object = Nothing
Dim filePath As String = "C:\Vault WS\Fichiers importants\Barcode.xlsx" ' Fichier Excel pour les numéros

' Vérifier si le fichier Excel existe, sinon le créer
If System.IO.File.Exists(filePath) Then
    workbook = excelApp.Workbooks.Open(filePath)
    worksheet = workbook.Sheets(1)
    ' Charger les numéros déjà utilisés dans la liste
    Dim i As Long = 1
    While worksheet.Cells(i, 1).Value IsNot Nothing
        numeration.Add(CLng(worksheet.Cells(i, 1).Value)) ' Convertir chaque valeur Excel en Long et l'ajouter à la liste
        i += 1
    End While
Else
    workbook = excelApp.Workbooks.Add()
    worksheet = workbook.Sheets(1)
End If

' Génération du numéro aléatoire unique
Do
    ' Utiliser NextDouble pour obtenir un nombre décimal puis multiplier pour obtenir un grand nombre
    numeroUtilise = CLng(random.NextDouble() * xxx-xxxxxxxx) ' Générer un numéro entre 0 et 10 milliards
Loop While numeration.Contains(numeroUtilise) ' Vérifie si le numéro existe déjà dans Excel

' Récupération du document de dessin actif
Dim miseEnPlan As DrawingDocument = ThisApplication.ActiveDocument
Dim feuille As Sheet = miseEnPlan.ActiveSheet

' Création d'un point d'insertion
Dim positionX As Double = 10 ' Position X du texte
Dim positionY As Double = 10 ' Position Y du texte
Dim pointInsertion As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(positionX, positionY)

' Ajout de la note de texte avec le numéro généré
texte = numeroUtilise.ToString() ' Utilisation du numéro généré comme texte de la note
Dim noteTexte As GeneralNote = feuille.DrawingNotes.GeneralNotes.AddFitted(pointInsertion, texte, "Barcode")

' Configuration des propriétés du texte via TextStyle
Dim styleTexte As TextStyle = noteTexte.TextStyle
styleTexte.FontSize = 0.5 ' Taille du texte
styleTexte.Font = "3 of 9 Barcode" ' Police de texte
noteTexte.TextStyle = styleTexte

' Centrage du texte
noteTexte.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
noteTexte.VerticalJustification = VerticalTextAlignmentEnum.kAlignTextMiddle

' Couleur du texte (en noir ici)
noteTexte.Color = ThisApplication.TransientObjects.CreateColor(0, 0, 0)

' Trouver la première ligne vide pour ajouter le numéro
Dim lastRow As Long = worksheet.Cells(worksheet.Rows.Count, 1).End(-4162).Row + 1 ' -4162 correspond à xlUp pour aller à la dernière ligne remplie et ajouter 1
worksheet.Cells(lastRow, 1).Value = numeroUtilise ' Ajouter le numéro utilisé à la première cellule vide

' Sauvegarder et fermer Excel
workbook.SaveAs(filePath)
excelApp.Quit()

' Affichage d'un message de confirmation
System.Windows.Forms.MessageBox.Show("Le numéro généré est : " & numeroUtilise.ToString(), "Succès", MessageBoxButtons.OK, MessageBoxIcon.Information)