iLogic Drawing Index from Sheet Name and Number

iLogic Drawing Index from Sheet Name and Number

sosborne
Explorer Explorer
5,104 Views
4 Replies
Message 1 of 5

iLogic Drawing Index from Sheet Name and Number

sosborne
Explorer
Explorer

I am looking for a reasonably straightforward way to take the sheet names and sheet numbers from a drawing set and automatically put them into a table on sheet one of an Inventor drawing.

 

I am using the code below to link a titleblock field to the sheet title so the sheet has a global title and a specific per-sheet title/description

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

Dim oTitleBlock As TitleBlock
Dim oTextBox As TextBox
Dim oSheet As Sheet

Dim lPos As Long
Dim sSheetName As String
 
For Each oSheet In oDoc.Sheets
	If oSheet.ExcludeFromCount = False Then
	    oTitleBlock = oSheet.TitleBlock
		lPos = InStr(oSheet.Name, ":")
		sSheetName = Left(oSheet.Name, lPos - 1)
		For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
	        If oTextBox.Text = "SHEET-NAME" Then
	            Call oTitleBlock.SetPromptResultText(oTextBox, sSheetName)
				
			End If
	    Next
		Else
	End If
Next

I considered exporting the sheet names and numbers to an excel file and then re-importing them but that seemed like it left more room for something to go wrong.

 

That said, I am stuck on moving that data into a table with a title (drawing index), 2 columns (Description and Sheet#) and adds as many rows as needed. I found a thread that referenced this code that I tried to modify but I am not able to get anything to display. Set up table with iLogic 

Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDoc.ActiveSheet
Dim sSheetName As String

oDoc = ThisDoc.Document
oSheet = oDoc.ActiveSheet
Dim oColumns As Integer
Dim oRows As Integer
oColumns = 2
i = 0

'tally the sheets that are set to be excluded
For Each oSheet In oDoc.Sheets
If oSheet.ExcludeFromCount = True Then
i = i + 1
Else
End If
Next

'subtract the excluded sheet from the number of sheets in the sheet collection
oMySheetCount = oDoc.Sheets.Count - i

oRows = 1 + oMySheetCount


'calculate number of columns cells
oCells = oColumns * oRows

Dim oTitles(oColumns - 1) As String

'Hardcoded titles 
oTitles(0) = "DESCRIPTION"
oTitles(1) = "SHEET #"


Dim oContents(oCells - 1) As String

Dim j As Integer
j = 0
i = 1
Do Until j = oCells
	'fill cell data
	If j = 0 Then
		oContents(j) = "Groef"
	ElseIf j = 6 Then
		oContents(j) = "Zetting"
	ElseIf j = 7 Then
		oContents(j) = "Aanslag"
	ElseIf j = 8 Then
		oContents(j) = "Zet Lengte"
	ElseIf j = 9 Then
		oContents(j) = i
		i = i + 1
	ElseIf j > 9
		If j = 9 + (i - 1) * 3 Then
			oContents(j) = i
			i = i + 1
		Else
			oContents(j) = ""
		End If
	Else
		oContents(j) = ""
	End If
	j = j + 1
Loop

Dim InsP As Point2d
InsP = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

Dim oCustomTable As CustomTable
oCustomTable = oSheet.CustomTables.Add("DRAWING INDEX", InsP, oColumns, oRows, oTitles, oContents)

I do not get any errors but I also do not get a table on the sheet or in the styles menu. In this application I think the loop is not needed but I wanted to see if I could get the code to work before stripping it out so I have left it in place for now.

 

Thanks,

Sean

 

0 Likes
Accepted solutions (2)
5,105 Views
4 Replies
Replies (4)
Message 2 of 5

A.Acheson
Mentor
Mentor
Accepted solution

The table was being added on a different sheet other than sheet 1. Here is a working example. Hopefully you can follow it's construction. Any questions I will be happy to help. 

Dim oDoc As DrawingDocument 
oDoc = ThisApplication.ActiveDocument

Dim oSheet, oIndexSheet As Sheet 
Dim sSheetName, sSheetNo As String
Dim oTitleBlock As TitleBlock
Dim oTextBox As TextBox

oIndexSheet = oDoc.Sheets(1)'In order to put table on sheet 1
oIndexSheet.Activate
oIndexSheet.ExcludeFromCount = True
'[Table Setup
Dim oColumns,oRows,oCells,i As Integer
oColumns = 2
i = 0
oRows = oDoc.Sheets.Count - 1
oCells = oColumns * oRows 'calculate number of cells

Dim oContents(oCells - 1) As String 'Minus 1 as array will start from zero
Dim oTitles(oColumns - 1) As String 'Minus 1 as array will start from zero
'Hardcoded titles 
oTitles(0) = "DESCRIPTION"
oTitles(1) = "SHEET #"
']

For Each oSheet In oDoc.Sheets
	If oSheet.ExcludeFromCount = False Then 'Avoid processing index page
		lPos = InStr(oSheet.Name, ":")
		sSheetName = Left(oSheet.Name, lPos - 1)
		sSheetNo = Right(oSheet.Name, 1)
		
		oTitleBlock = oSheet.TitleBlock
		
		For Each oTextBox In oTitleBlock.Definition.Sketch.TextBoxes
	        If oTextBox.Text = "SHEET-NAME" Then
	            Call oTitleBlock.SetPromptResultText(oTextBox, sSheetName)
			End If
	    Next
		'[Table Data
		oContents(i) = sSheetName 'Add to string 
		i = i+1
		oContents(i) = sSheetNo  'Add to string 
		i = i+1
		']
	Else

	End If
Next

Dim InsP As Point2d
InsP = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

Dim oCustomTable As CustomTable
oCustomTable = oIndexSheet.CustomTables.Add("DRAWING INDEX", InsP, oColumns, oRows, oTitles, oContents)

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 5

sosborne
Explorer
Explorer

That worked brilliantly, thanks so much! I am embarrassed that I missed the table being on the wrong sheet. Is there a way to do a conditional statement that checks for an existing table and either deletes/replaces it or updates? Something like

if; table exists then update/delete table

else; create table

 

Thanks,

Sean

 

0 Likes
Message 4 of 5

A.Acheson
Mentor
Mentor
Accepted solution

Something like check for count of tables in the sheet with certain title then delete if found. This involves looping through the sheets. 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the active sheet.
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet

For i = 1 To oSheet.CustomTables.Count
    TableTitle = oSheet.CustomTables.Item(i).Title                       
    If TableTitle = "ASSEMBLY ORDERS" Then
        TableNr = i
	oSheet.CustomTables.Item(i).Delete
        GoTo CreateTable
    End If
Next

'if not found go create it
CreateTable:
'Declare my custom table
Dim oCustomTable As CustomTable

'*************** Add table here***********

if you know where the table is by giving the sheet number and there is only one custom table  then delete directly. You should be able to tack the if statement check if exists portion on to the end of your rule just before table creation. 

Dim oDoc As DrawingDocument 
oDoc = ThisApplication.ActiveDocument

Dim oSheet, oIndexSheet As Sheet 
oIndexSheet = oDoc.Sheets(1)'In order to put table on sheet 1

Dim oCustomTable As CustomTable
If oIndexSheet.CustomTables.Count = 1 Then
	oCustomTable = oIndexSheet.CustomTables.Item(1)
	oCustomTable.Delete
Else

End If

'oCustomTable = oIndexSheet.CustomTables.Add("DRAWING INDEX", InsP, oColumns, oRows, oTitles, oContents)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 5

farshidDWRFR
Enthusiast
Enthusiast

Hello everyone,

 

Not sure if this is the right place to ask this question, so please let me know if it is not.

I am trying to write an iLogic snippet that asks the user for a number as the start page number for a document containing multiple drawing pages and then changes sheet numbers' beginning to that number. For example, I want my drawing sheet numbers to start from 20 instead of 1. I would like to use the sheet number in the title block afterwards.

 

Thanks in advance for your help

0 Likes