How to set column width using iLogic

How to set column width using iLogic

harveylanc
Contributor Contributor
775 Views
8 Replies
Message 1 of 9

How to set column width using iLogic

harveylanc
Contributor
Contributor

Hello,

 

I am new to iLogic and I found this code in the forum and it appears to work perfectly but I cannot figure out how to get the columns to fit the text width automatically. When the table imports it wraps the text onto another row when it's too wide for the cell.

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
    
    Dim oActiveSheet As Sheet = oDrawDoc.ActiveSheet
    
    ' Create the placement point for the table
    Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(36.6,11)
    
    Dim oExcelTable As CustomTable = oActiveSheet.CustomTables.AddExcelTable("C:\Temp\test.xls", oPoint, "Excel Table")

 Can someone tell me how to modify the code so that the column width automatically fits to the width of the text when they are imported into the drawing or can this only be a manual process of widening the columns?

0 Likes
Accepted solutions (1)
776 Views
8 Replies
Replies (8)
Message 2 of 9

harvey3ELEA
Advocate
Advocate

This code sets my parts list column widths to best fit (also borrowed from this forum a while ago).

Does this one do the trick?

 

'Public Sub FitColumnsWidth(oPartsList As PartsList)
Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	For Each oSheet As Inventor.Sheet In oDDoc.Sheets
		For Each oPList As PartsList In oSheet.PartsLists
			Dim oPPoint As Point2d = oTG.CreatePoint2d(0,0)
			Dim oTempGNote As Inventor.GeneralNote = oSheet.DrawingNotes.GeneralNotes.AddFitted(oPPoint, "", oPList.DataTextStyle)
			Dim oColQty As Integer = oPList.PartsListColumns.Count
			Dim oWidths(oColQty) As Double
			Dim i As Integer
			For i = 0 To oColQty - 1
				Dim oNext As Integer = i + 1
				oTempGNote.Text = oPList.PartsListColumns(oNext).Title
				oWidths(i) = oTempGNote.FittedTextWidth
				For Each oRow As PartsListRow In oPList.PartsListRows
					If oRow.Visible Then
						oTempGNote.Text = oRow(oNext).Value
						If oTempGNote.FittedTextWidth > oWidths(i) Then
							oWidths(i) = oTempGNote.FittedTextWidth
						End If
					End If
				Next
				oPList.PartsListColumns(oNext).Width = oWidths(i) + oPList.DataTextStyle.FontSize
			Next
			oTempGNote.Delete()
		Next
	Next
End Sub
'https://forums.autodesk.com/t5/inventor-programming-ilogic/auto-fit-columns-in-parts-list/td-p/9647846/page/2_MESSAGE37

This  

0 Likes
Message 3 of 9

harveylanc
Contributor
Contributor

Hi harvey3ELEA,

 

To be honest, I am so new to iLogic I do not know how to merge the 2 pieces of code together to make it do a "best fit". I'll take a few attempts at it and let you know for sure.

0 Likes
Message 4 of 9

GosponZ
Collaborator
Collaborator
Accepted solution

Try this one. You have to make multi parameter PartsListStyle with different table styles, something like Laser, Plasma, Router etc... and when you run this rule it will place into right hand upper corner parts list.

Sub Main()

'Declare local variables
Dim oDrawDoc As DrawingDocument
Dim oSheet As Sheet
Dim oDrawingView As DrawingView
Dim oPlacementPoint As Point2d
Dim oPartsList As PartsList 
Dim oBorder As Border
Dim oStyle As String
Dim oPlaceX As Double
Dim oPlaceY As Double

'initialize variables
oDrawDoc = ThisApplication.ActiveDocument
oSheet = oDrawDoc.ActiveSheet
oDrawingView = oSheet.DrawingViews(1)
oBorder = oSheet.Border

'user input - select parts list style
'oStyle = InputListBox("Choose Parts List Style", MultiValue.List("PartsListStyle"), "Custom Parts List (CUSTOMER)","Parts List Style", "List Prompt") 
oStyle = InputListBox("Choose Parts List Style", MultiValue.List("PartsListStyle"),"Parts List Style", "List Prompt") 

'create parts list
oPartsList = oSheet.PartsLists.Add(oDrawingView, oBorder.RangeBox.MinPoint)
oPartsList.Style = oDrawDoc.StylesManager.PartsListStyles.Item(oStyle)

'reposition parts list to top left
oPlaceX = oBorder.RangeBox.MaxPoint.X' - (oPartsList.RangeBox.MaxPoint.X + oPartsList.RangeBox.MinPoint.X)'+(1.27)
oPlaceY = oBorder.RangeBox.MaxPoint.Y 
oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oPlaceX,oPlaceY)
oPartsList.Position = oPlacementPoint

End Sub





 

 

0 Likes
Message 5 of 9

harvey3ELEA
Advocate
Advocate

No need to merge the code I posted with anything else.

Simply copy that code into a "Create New External Rule" (right click on Standard Directories under iLogic tab in model tree) with a saved name you prefer.

0 Likes
Message 6 of 9

harveylanc
Contributor
Contributor

Thanks everyone,

 

Your explanations helped me to understand more about how it works. The code GosponZ provided appears to work fine however, the code I originally posted allows me to use an Excel spreadsheet as the source for the data in the table which I liked. I know that Inventor has the ability to link the spreadsheet during the general table insertion but I was trying to automate it. I will keep testing the code with modifications and see if I can move in the direction of a list and form so to speak so I can learn more. I appreciate your help.

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

The original post contained code that is adding a 'CustomTable' to a drawing, not a PartsList.  When we use the CustomTables.AddExcelTable method, we do not have any initial opportunity to specify how wide we want the columns to be, unlike when we use the CustomTables.Add method.  So, when we have used the AddExcelTable method, we capture the new CustomTable object which that method creates for us as the value of a CustomTable Type variable.  Then, we can set the column widths individually, following the following object/property trail:

CustomTable.Columns.Item(index).Width

('index' can be replaced with an Integer indicating its Index in the collection, or the column header/title)

 That Width Property is expecting a Double type value (number with decimal point).  Its value will likely be interpreted as centimeters, since that is the 'database' (system default) units for distance, instead of 'document units' (if different).  So, we could iterate through Each column in the CustomTable.Columns collection, and attempt to set them all to the same value, or access them individually by their column header/title, and set a different, specific value to each one.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

harveylanc
Contributor
Contributor

Thanks WCrihfield,

 

That makes sense. I was able to modify my original code and that set the width as you suggested. This is the code and it works!

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
    
    Dim oActiveSheet As Sheet = oDrawDoc.ActiveSheet
    
    ' Create the placement point for the table
    Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(36.6,11)
    
    Dim oExcelTable As CustomTable = oActiveSheet.CustomTables.AddExcelTable("C:\Temp\test.xls", oPoint, "Excel Table")
   'Use the integer of the column
oExcelTable.Columns(1).Width = 4.445
oExcelTable.Columns(2).Width = 7.62
   ' Hide the table title
    oExcelTable.ShowTitle = False

Do you know if it is possible to have to automatically place in the upper right corner of the border or do I need to keep adjusting the "CreatePoint2d" to get it there or just manually drag it in place so it snaps?

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

It has been a while since I created a CustomTable by code, but I believe their initial placement location goes by its upper, left corner.  So, if you need the upper, right corner of the CustomTable to be at the upper, right corner of the drawing sheet's border, some measuring, then repositioning would need to be involved, after it was placed on the sheet somewhere else.  We can get that location using the following object/property trail:

Sheet.Border.RangeBox.MaxPoint

...where Sheet is the drawing sheet object, and Max Point gives us an Inventor.Point2d object, which will be at the upper, right (max in positive X direction, and max in positive Y direction) corner of the border's sketch geometry.

So, place the table somewhere random first, then use the CustomTable.RangeBox.MaxPoint property to get its upper, right corner position, then measure how far that is from the upper, right corner of the border, then reposition the table using a 'new' modified point object, that resulted from that math from the X & Y values of the points.

CustomTable.RangeBox 

Border.RangeBox 

Box2d 

Box2d.MaxPoint 

Point2d 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes