Create text via iLogic?

Create text via iLogic?

Anonymous
Not applicable
3,082 Views
3 Replies
Message 1 of 4

Create text via iLogic?

Anonymous
Not applicable

Hello,

I am looking for a way to create text in an .idw using iLogic. I've already searched the program's help menu and programming guides, found a code that is even referenced in the forums here from 2014, but the code in each instance is no longer supported in v2018. Sample of the code is below:

 

Sub SketchTextAdd()
    ' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    
    ' Create a new sketch on the active sheet.
    Dim oSketch As DrawingSketch
    Set oSketch = oDrawDoc.ActiveSheet.Sketches.Add
    
    ' Open the sketch for edit so the text boxes can be created.
    ' This is only required for drawing sketches, not part.
    oSketch.Edit
    
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
    
    ' Create text with simple string as input.  Since this doesn't use
    ' any text overrides, it will default to the active text style.
    Dim sText As String
    sText = "Drawing Notes"
    Dim oTextBox As TextBox
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 18), sText)
    
    ' Create text using various overrides.
    sText = "Notice: All holes larger than 0.500 n are to be lubricated."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 16), sText)
    
    ' Create a set of notes that are numbered and aligned along the left.
    Dim dYCoord As Double
    dYCoord = 14
    Dim dYOffset As Double
    Dim oStyle As TextStyle
    Set oStyle = oSketch.TextBoxes.Item(1).Style
    dYOffset = oStyle.FontSize * 1.5
    
    ' Simple single line text.
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "1.")
    sText = "This is note 1."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Two line text.  The two lines are defined using the  tag within the text string.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "2.")
    sText = "This is note 2,  which contains two lines."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Single line of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "3.")
    sText = "This is note 3."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Three lines of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "4.")
    sText = "This is note 4,  which contains  several lines."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' The number of this line of text will have a triangle around it.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "5")
    
    ' Draw a triangle that fits around a text box.
    Dim Points(1 To 3) As Point2d
    Call CalculateTriangle(oTextBox, Points)
    
    Dim oLines(1 To 3) As SketchLine
    Set oLines(1) = oSketch.SketchLines.AddByTwoPoints(Points(1), Points(2))
    Set oLines(2) = oSketch.SketchLines.AddByTwoPoints(oLines(1).EndSketchPoint, Points(3))
    Set oLines(3) = oSketch.SketchLines.AddByTwoPoints(oLines(2).EndSketchPoint, oLines(1).StartSketchPoint)
        
    sText = "Here is the last and final line of text."
    Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
        
    ' Exit the sketch from the edit environment.
    oSketch.ExitEdit
End Sub

 

When I run it, nearly every line states "Let" and "Set" are no longer supported... This is the only code I can find online as well. How can this be accomplished now in v2018? Thanks.

0 Likes
Accepted solutions (1)
3,083 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

Did some digging around and removed "Set " (including the space after the word without quotes), fixed one typo in the original code, and changed (1 to 3) to (0 to 2). The code runs now. After it creates the sketch and places each line, an error pops up:

 

Error in rule: TEST2, in document: Sheet Numbering.idw

 

Object reference not set to an instance of an object.

 

 

Anyone have any ideas where this is originating from?

The sketch completes and the text stays but with this error.

 

The modified code:

Public Sub Main()
Public Sub SketchTextAdd()
    ' a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
		oDrawDoc = ThisApplication.ActiveDocument
    
    ' Create a new sketch on the active sheet.
    Dim oSketch As DrawingSketch
		oSketch = oDrawDoc.ActiveSheet.Sketches.Add
    
    ' Open the sketch for edit so the text boxes can be created.
    ' This is only required for drawing sketches, not part.
    oSketch.Edit
    
    Dim oTG As TransientGeometry
    	oTG = ThisApplication.TransientGeometry
    
    ' Create text with simple string as input.  Since this doesn't use
    ' any text overrides, it will default to the active text style.
    Dim sText As String
    sText = "Drawing Notes"
    Dim oTextBox As TextBox
    	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 18), sText)
    
    ' Create text using various overrides.
    sText = "Notice: All holes larger than 0.500 n are to be lubricated."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 16), sText)
    
    ' Create a of notes that are numbered and aligned along the left.
    Dim dYCoord As Double
    	dYCoord = 14
		
    Dim dYOff As Double
    Dim oStyle As TextStyle
    	oStyle = oSketch.TextBoxes.Item(1).Style
    	dYOff= oStyle.FontSize * 1.5
    
    ' Simple single line text.
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "1.")
    sText = "This is note 1."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Two line text.  The two lines are defined using the  tag within the text string.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "2.")
    sText = "This is note 2,  which contains two lines."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Single line of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "3.")
    sText = "This is note 3."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Three lines of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "4.")
    sText = "This is note 4,  which contains  several lines."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' The number of this line of text will have a triangle around it.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "5")
    
    ' Draw a triangle that fits around a text box.
    Dim Points(0 To 2) As Point2d
    Call CalculateTriangle(oTextBox, Points)
    
    Dim oLines(0 To 2) As SketchLine
		oLines(0) = oSketch.SketchLines.AddByTwoPoints(Points(1), Points(2))
		oLines(1) = oSketch.SketchLines.AddByTwoPoints(oLines(1).EndSketchPoint, Points(3))
		oLines(2) = oSketch.SketchLines.AddByTwoPoints(oLines(2).EndSketchPoint, oLines(1).StartSketchPoint)
        
    sText = "Here is the last and final line of text."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
        
    ' Exit the sketch from the edit environment.
    oSketch.ExitEdit
End Sub

' Function to calcuate the three points of a triangle that fits tightly around the input text box.
Private Sub CalculateTriangle(TextBox As TextBox, Points() As Point2d)
    ' Get the top-left corner of the text box.
    Dim dLeft As Double
    Dim dTop As Double
    Call GetCorner(TextBox, dLeft, dTop)
    
    Dim oTG As TransientGeometry
    oTG = ThisApplication.TransientGeometry
   
    ' Determine the top point of the triangle.
    Points(0) = oTG.CreatePoint2d(dLeft + (TextBox.Width / 2), dTop + ((TextBox.Width / 2) * Tan(1.0471975511966)))
    
    ' Determine the bottom-left corner point of the triangle.
    Dim dY As Double
    dY = (Points(0).Y - dTop) + (1.125 * TextBox.Height)
    Points(1) = oTG.CreatePoint2d(Points(0).X - (dY / Tan(1.0471975511966)), Points(1).Y - dY)
    
    ' Determine the bottom right corner point of the triangle
    Points(2) = oTG.CreatePoint2d(Points(0).X + (dY / Tan(1.0471975511966)), Points(2).Y)
End Sub

' Function to determine the top left corner of the input text box.
Private Sub GetCorner(TextBox As TextBox, Left As Double, Top As Double)
    ' Determine the top left corner of the text box by accounting
    ' for the justifications.
    Select Case TextBox.HorizontalJustification
        Case kAlignTextLeft
            Left = TextBox.Origin.X
        Case kAlignTextCenter
            Left = TextBox.Origin.X - (TextBox.Width / 2)
        Case kAlignTextRight
            Left = TextBox.Origin.X - TextBox.Width
    End Select

    Select Case TextBox.VerticalJustification
        Case kAlignTextUpper
            Top = TextBox.Origin.Y
        Case kAlignTextMiddle
            Top = TextBox.Origin.Y + (TextBox.Height / 2)
        Case kAlignTextLower
            Top = TextBox.Origin.Y + TextBox.Height
    End Select
End Sub
0 Likes
Message 3 of 4

Anonymous
Not applicable

One step closer to the final goal, but the code is functioning without any error! Fixed the error (caused by having a blank drawing I assume)...

I removed the last two Private Sub calls and the segment of SketchTextAdd calling for any portion of the Triangle call/Triangle line generation.

 

Public Sub Main()
Public Sub SketchTextAdd()
    ' a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
		oDrawDoc = ThisApplication.ActiveDocument
    
    ' Create a new sketch on the active sheet.
    Dim oSketch As DrawingSketch
		oSketch = oDrawDoc.ActiveSheet.Sketches.Add
    
    ' Open the sketch for edit so the text boxes can be created.
    ' This is only required for drawing sketches, not part.
    oSketch.Edit
    
    Dim oTG As TransientGeometry
    	oTG = ThisApplication.TransientGeometry
    
    ' Create text with simple string as input.  Since this doesn't use
    ' any text overrides, it will default to the active text style.
    Dim sText As String
    sText = "Drawing Notes"
    Dim oTextBox As TextBox
    	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 18), sText)
    
    ' Create text using various overrides.
    sText = "Notice: All holes larger than 0.500 n are to be lubricated."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, 16), sText)
    
    ' Create a of notes that are numbered and aligned along the left.
    Dim dYCoord As Double
    	dYCoord = 14
		
    Dim dYOff As Double
    Dim oStyle As TextStyle
    	oStyle = oSketch.TextBoxes.Item(1).Style
    	dYOff= oStyle.FontSize * 1.5
    
    ' Simple single line text.
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "1.")
    sText = "This is note 1."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Two line text.  The two lines are defined using the  tag within the text string.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "2.")
    sText = "This is note 2,  which contains two lines."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Single line of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "3.")
    sText = "This is note 3."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    ' Three lines of text.
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "4.")
    sText = "This is note 4,  which contains  several lines."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
    
    dYCoord = dYCoord - (oTextBox.FittedTextHeight + 0.5)
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(3, dYCoord), "5")
        
    sText = "Here is the last and final line of text."
    oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(4, dYCoord), sText)
        
    ' Exit the sketch from the edit environment.
    oSketch.ExitEdit
End Sub
0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

Posting the final code here for public use. Tweak as you need 🙂
This Rule code as it is below will provide Continuous zone numbering for an F size sheet with 1/2" border, A1-F8 Zones (numbers only, no alpha).

Enjoy!

 

Public Sub Main Generator()
Public Sub SketchTextAdd()

oDocument = ThisDoc.Document
ShNum  = Mid(oDocument.ActiveSheet.Name, InStr(1, oDocument.ActiveSheet.Name, ":") + 1)
mymultiplier = "8" 'Number of zones for the sheet along top and bottom edges.
Zone01 = (ShNum * mymultiplier) - 7
Zone02 = (ShNum * mymultiplier) - 6
Zone03 = (ShNum * mymultiplier) - 5
Zone04 = (ShNum * mymultiplier) - 4
Zone05 = (ShNum * mymultiplier) - 3
Zone06 = (ShNum * mymultiplier) - 2
Zone07 = (ShNum * mymultiplier) - 1
Zone08 = (ShNum * mymultiplier) - 0

    Dim oDrawDoc As DrawingDocument
		oDrawDoc = ThisApplication.ActiveDocument

    Dim oSketch As DrawingSketch
		oSketch = oDrawDoc.ActiveSheet.Sketches.Add

    oSketch.Edit
    
    Dim oTG As TransientGeometry
    	oTG = ThisApplication.TransientGeometry

    Dim sText As String
    sText = "Drawing Notes"
    Dim oTextBox As TextBox
    	oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(0, 0), "")
		
	Dim SF As Double
		SF = 2.5393600812595226003047232097511
	Dim SO As Double
		SO = 2.8312570781426953567383918459796
		Dim Adj1 As Double
			Adj1 = (-5) * SO
		Dim Adj2 As Double
			Adj2 = .5156 * SO
			'Adj2 = (1/3) * SO
		Dim Adj0 As Double
			Adj0 = Adj1 + Adj2
	
	Dim X8A As Double
		X8A = 2.5 * SF
	Dim X8F As Double
		X8F = 2.5 * SF
	Dim YA As Double
		YA = (.08+.275) * SF
	Dim YF As Double
		YF = (.07+27.75) * SF

	oTextBox08A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone08)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox07A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone07)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox06A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone06)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox05A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone05)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox04A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone04)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox03A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone03)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox02A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone02)
	X8A = X8A - (oTextBox.FittedTextHeight + Adj0)
		oTextBox01A = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8A, YA), Zone01)
	
	oTextBox08F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone08)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox07F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone07)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox06F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone06)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox05F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone05)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox04F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone04)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox03F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone03)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox02F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone02)
	X8F = X8F - (oTextBox.FittedTextHeight + Adj0)
		oTextBox01F = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(X8F, YF), Zone01)
    
    ' Exit the sketch from the edit environment.
    oSketch.ExitEdit
End Sub
0 Likes