Is there a way to control text Stretch(%)? Trying to automate number plates

Is there a way to control text Stretch(%)? Trying to automate number plates

Preston_Reed
Advocate Advocate
1,049 Views
9 Replies
Message 1 of 10

Is there a way to control text Stretch(%)? Trying to automate number plates

Preston_Reed
Advocate
Advocate

Hello, im trying to automate the creation of number plates based off of an excel sheet of names.  I have it working well, it will make a new part, insert the new text and save it.  The only issue, well I just want it to look a little better truly, but I would like to be able to control the text stretch.  Some of the text is longer than others so I have it set at 150% stretch in order to fit the largest plate.

 

Preston_Reed_0-1692307593028.pngPreston_Reed_2-1692307674943.png

Preston_Reed_3-1692307709470.png

 

 

0 Likes
Accepted solutions (1)
1,050 Views
9 Replies
Replies (9)
Message 2 of 10

Andrii_Humeniuk
Advisor
Advisor

Hi @Preston_Reed . First, make sure that the sketch in which the TextBox is located is called "Sketch2" (line 2) and edit it if necessary. In line 3, the program takes the first TextBox in the sketch. Lines 5-12 are a selection based on the number of characters in the text. You need to scale the text to different number of characters according to your situation, I chose the scale arbitrarily.

Dim oDoc As PartDocument = ThisDoc.Document
Dim oSketch As PlanarSketch = oDoc.ComponentDefinition.Sketches("Sketch2")
Dim oText As Inventor.TextBox = oSketch.TextBoxes(1)
Dim oBox As Box2d = oText.RangeBox
Select Case oText.Text.Length
Case 10 : oText.WidthScale = 1.2
Case 11 : oText.WidthScale = 1.3
Case 12 : oText.WidthScale = 1.4
Case 13 : oText.WidthScale = 1.5
Case 14 : oText.WidthScale = 1.6
Case 15 : oText.WidthScale = 1.7
End Select

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 10

gerrardhickson
Collaborator
Collaborator

Yeah - looks like you adjust the 'widthscale' property.

 

Sub TextStretch()

Dim oD As PartDocument
Dim oS As PlanarSketch
Dim oT As TextBox

Set oD = ThisApplication.ActiveDocument
Set oS = oD.ComponentDefinition.Sketches(1)
Set oT = oS.TextBoxes(1)

oT.WidthScale = 0.5

End Sub
0 Likes
Message 4 of 10

Preston_Reed
Advocate
Advocate

Hey im getting error messages trying to access .Sketches from the assembly document, i was looking in the API object model, i dont think i can access sketches from the assembly.  

 

Preston_Reed_0-1692385350077.png

 

 

Private Sub CopyPipes()

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oSketch As PlanarSketch = oDoc.AssemblyComponentDefinition.Sketches("TextSketch")    '***Line 33
Dim oText As Inventor.TextBox = oSketch.TextBoxes(1)
Dim oBox As Box2d = oText.RangeBox

GoExcel.Open(FileLocation, "Sheet1")

Dim i As Integer = 0

'index Row 2 through 10000
For rowPN = 3 To 100
'find first empty cell in column A
 If GoExcel.CellValue("P:\MCI-METAL RACK\SHEET METAL RACK LABELS.xlsx", "Sheet1", "A1") = "" Then
'create a variable for the cell value that is one row less than the empty cell row
    lastPN = GoExcel.CellValue("P:\MCI-METAL RACK\SHEET METAL RACK LABELS.xlsx", "Sheet1", "A" & rowPN - 1)
		If lastPN.Length() = 9 Then 
			oText.WidthScale = 2
		ElseIf lastPN.Length() = 10 Then 
			oText.WidthScale = 1.9
		ElseIf lastPN.Length() = 11 Then
			oText.WidthScale = 1.8
		ElseIf lastPN.Length() = 12 Then
			oText.WidthScale = 1.7
		ElseIf lastPN.Length() = 13 Then
			oText.WidthScale = 1.6
		End If 
		
		If lastPN = "" Then
        	Exit For
		End If
 End If
 Parameter("OrginalPart:1", "Text") = lastPN
 oDoc.SaveAs("P:\MCI-METAL RACK\Parts" & "/" & lastPN & ".ipt", True)

Next
End Sub	

 

0 Likes
Message 5 of 10

Frederick_Law
Mentor
Mentor

Program is not magic.

It just repeat what you manually do, with more detail usually.

So how would you edit a sketch of a part in an assembly?

Message 6 of 10

Preston_Reed
Advocate
Advocate

Well its an assembly file with only 1 part file in it.  The goal is to duplicate that part & replace the text every iteration based off of an excel chart.  I have all of that works just fine, I'm trying to add the ability to control the stretch of the text based off of the string length.  

 

This works code works just fine for what I'm trying to do.  Just trying to add the ability to control stretch now.  When i try to access PlanarSketch via part document i get an error & same with assembly document.  I'm just not sure why i cant access it via part document, it should be there, maybe my syntax is wrong 

 

Preston_Reed_0-1692391191339.png

 

Sub main
'	MakeParameters
	SetParameters
	CopyPipes
	RuleParametersOutput()
	iLogicVb.DocumentUpdate()
End Sub

Sub MakeParameters
	oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
	'Setting an userParameter as text parameter
	oParameter=oMyParameter.AddByValue("FileLocation", "", UnitsTypeEnum.kTextUnits)
End Sub


Sub SetParameters
	'Promt the user for the value of a variable
	Input1 = InputBox("FileLocation", "FileLocation: ", "")
	'assign the value of that variable to user parameter
	Parameter.UpdateAfterChange = True
	Parameter("FileLocation") = ""
	Parameter.UpdateAfterChange = True
	Parameter("FileLocation") = Input1
End Sub

Private Sub CopyPipes()

Dim oPart = Component.InventorComponent("OrginalPart:1")
Dim oPartDoc As PartDocument = oPart.Definition.Document
Dim oPath As String = ThisDoc.Path

GoExcel.Open(FileLocation, "Sheet1")

Dim i As Integer = 0

'index Row 2 through 1000
For rowPN = 3 To 1000
'find first empty cell in column A
 If GoExcel.CellValue("P:\MCI-METAL RACK\SHEET METAL RACK LABELS.xlsx", "Sheet1", "A1") = "" Then
'create a variable for the cell value that is one row less than the empty cell row
    lastPN = GoExcel.CellValue("P:\MCI-METAL RACK\SHEET METAL RACK LABELS.xlsx", "Sheet1", "A" & rowPN - 1)
		If lastPN = "" Then
        	Exit For
		End If
 End If
 Parameter("OrginalPart:1", "Text") = lastPN
 oPartDoc.SaveAs("P:\MCI-METAL RACK\Parts" & "/" & lastPN & ".ipt", True)
Next
End Sub	

 

0 Likes
Message 7 of 10

Frederick_Law
Mentor
Mentor

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-D54193DE-A29F-4336-B266-F2F14410E11C

 

Usually it's difficult to find what you need in API.

Sometime I use VBA.

Set breakpoint, add Watch and go through objects to find what I need.

Message 8 of 10

gerrardhickson
Collaborator
Collaborator

I agree with @Frederick_Law. Debug it in VBA and then migrate it to .Net once it's working.

 

There's a few odd things in your code. Make sure your defining everything fully. oPart appears to be a componentoccurrence, but it's not defined as such.

In your CopyPipes() Sub, it looks like oSketch is an assembly level sketch, not a part level sketch which would cause odd behaviour. 

If you can share the full code and a sample file it should be easy enough to diagnose.

0 Likes
Message 9 of 10

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @Preston_Reed . Check that the names and paths are correct in lines 8, 11-12, 28, 33, 40, 50. The rule should be run in your assembly, then select your excel and finally you will get your details.

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = oInvApp.ActiveDocument
	If Not TypeOf oDoc Is AssemblyDocument Then Exit Sub
	Dim oAsmDoc As AssemblyDocument = oDoc
	Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
	Dim oPart As ComponentOccurrence
	Try : oPart = oAsmDef.Occurrences.ItemByName("OrginalPart:1") : Catch : Exit Sub : End Try
	Dim oUParams As UserParameters = oAsmDef.Parameters.UserParameters
	Dim oParam As UserParameter
	Try : oParam = oUParams("FileLocation") : Catch
	oParam = oUParams.AddByValue("FileLocation", "", UnitsTypeEnum.kTextUnits) : End Try
	oParam.Value = GetFileLocation(oInvApp)
	If String.IsNullOrEmpty(oParam.Value) Then Exit Sub
	If Not IO.File.Exists(oParam.Value) Then Exit Sub
	Call CopyPipes(oPart, oParam.Value)
	oDoc.Update()
End Sub

Private Function GetFileLocation(ByVal oInvApp As Inventor.Application) As String
	Dim oFileDlg As Inventor.FileDialog = Nothing: oInvApp.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Excel File (*.xlsx;*.xls)|*.xlsx;*.xls"
	oFileDlg.ShowOpen()
	Return oFileDlg.FileName
End Function

Private Function CopyPipes(oPart As ComponentOccurrence, ByVal sFileLoc As String)
	Dim sNameSheet As String = "Sheet1"
	Dim i As Integer = 2
	Dim slastPN, sCellValue As String
	Dim oPartDef As PartComponentDefinition = oPart.Definition
	Dim oPartDoc As PartDocument = oPartDef.Document
	Dim oSketch As PlanarSketch = oPartDef.Sketches("TextSketch")
	Dim oText As Inventor.TextBox = oSketch.TextBoxes(1)
	
	GoExcel.Open(sFileLoc, sNameSheet)
	Do
		slastPN = GoExcel.CellValue(sFileLoc, sNameSheet, "A" & i)
		If Not String.IsNullOrEmpty(slastPN) Then
			oPartDef.Parameters.UserParameters("Text").Value = slastPN
			Select Case slastPN.Length()
			Case 9 : oText.WidthScale = 2
			Case 10 : oText.WidthScale = 1.9
			Case 11 : oText.WidthScale = 1.8
			Case 12 : oText.WidthScale = 1.7
			Case 13 : oText.WidthScale = 1.6
			Case 14 : oText.WidthScale = 1.5
			End Select
			oPartDoc.Update()
			oPartDoc.SaveAs("P:\MCI-METAL RACK\Parts\" & slastPN & ".ipt", True)
		End If
		i += 1
	Loop While Not String.IsNullOrEmpty(slastPN)
	GoExcel.Close()	
End Function	

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 10 of 10

Preston_Reed
Advocate
Advocate

@Andrii_Humeniuk thank you so much, it worked perfectly!  I like how you prompted the user to find the location of the file.  I had it as a prompt to copy and paste the file location, but yours opens up the workspace and you can select the file.  I'm still pretty new to iLogic and the API  so thank you for showing me that, ill be experimenting with that now for other applications! 

 

 

0 Likes