Need iLogic - VB help to delete sheets from an .idw file...

Need iLogic - VB help to delete sheets from an .idw file...

JamesNordgren
Advocate Advocate
1,949 Views
8 Replies
Message 1 of 9

Need iLogic - VB help to delete sheets from an .idw file...

JamesNordgren
Advocate
Advocate

Hi,

 

Can anyone help me with some code I can add to a drawing rule to delete a specific sheet from an idw file?

 

There is no direct command to do this in iLogic (2012 Pro - 64) so I am kind of stuck.  I know it can be done thru the API, I just dont know how to code it in an iLogic rule.

 

Thanks to Any who can help!

 

James

0 Likes
Accepted solutions (4)
1,950 Views
8 Replies
Replies (8)
Message 2 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi JNORDGREN,

This should do it:

 

Dim oSheet As Sheet
Dim oListofSheets As New ArrayList

For Each oSheet In ThisApplication.ActiveDocument.Sheets
oListofSheets.add(oSheet.Name)
Next
Dim sSheetToDelete as String
sSheetToDelete = InputListBox("Select the Sheet to Delete", oListofSheets, oListofSheets.Item(0), "iLogic", "List of Sheets")
oSheet = ThisApplication.ActiveDocument.Sheets.Item(sSheetToDelete) 
oSheet.Delete

 

If you get errors from copying and pasting directly from this forum, you can use the attached *.txt file.

 

 

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 3 of 9

JamesNordgren
Advocate
Advocate

Thanks for the quick response Curtis (I had a feeling You wd be first to chime in on this)

 

I may be able to modify what you gave me to make work,  but how about this:  I dont need to get a list of the sheets and then select which to delete.  I want to delete specific named sheets based on other parameters, and with no user input.

 

In words, what I want to code is:

 

If "boolean parameter 1" = True

     Delete Sheet1

Else If  "boolean parameter 2" = True

    Delete Sheet2

...

End If

 

... and so on like that.  Could you modify what you had written previously to work like that?

 

Thanks again

0 Likes
Message 4 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi JNORDGREN,

 

I don't follow how the boolean gets set without user input, but maybe this is something preset in a template or something? Here's an example with user input and a boolean:

 

Dim bSheetToDelete as Boolean
bSheetToDelete = InputRadioBox("Select a sheet to delete", "1st Sheet", "2nd Sheet", True, "ilogic")

Dim oSheet As Sheet
If bSheetToDelete = True Then
oSheet = ThisApplication.ActiveDocument.Sheets.Item(1)
oSheet.Delete
Else
    Try
    oSheet = ThisApplication.ActiveDocument.Sheets.Item(2)
    oSheet.Delete
    Catch
    MessageBox.Show("2nd sheet not found?", "iLogic")
    Return
    End Try
End If

 

Here's an example that expects to find a preset boolean parameter named MyBooleanParam in the drawing file:

 

 

Dim oSheet As Sheet
If MyBooleanParam = True Then
oSheet = ThisApplication.ActiveDocument.Sheets.Item(1) 
oSheet.Delete
Else
	Try 
	oSheet = ThisApplication.ActiveDocument.Sheets.Item(2) 
	oSheet.Delete
	Catch
	MessageBox.Show("2nd sheet not found?", "iLogic")
	Return
	End Try
End If

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 5 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

If the sheet name is alway the same you could use something like this:

 

Dim oSheet As Sheet
oSheetName = "Sheet:2"
Try 
oSheet = ThisApplication.ActiveDocument.Sheets.Item(oSheetName) 
oSheet.Delete
Catch
MessageBox.Show(oSheetName & " not found?", "iLogic")
Return
End Try

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 6 of 9

JamesNordgren
Advocate
Advocate

Thanks again Curtis,

 

Yes, the sheet name is a known constant, and the boolean for the IF statements is set by the user in a form which runs in an assembly rule.  Based on the options the user selects in the assembly, I want to delete certain sheets from a drawing.  I think I can get there now.

0 Likes
Message 7 of 9

To_Efficiency_and_Beyond
Enthusiast
Enthusiast

Hi,

In our office, other departments sometimes access our inventor .dwg files using AutoCAD. If the AutoCAD user places a block in the model space tab and then saves, there will be a new sheet next time the file is opened in Inventor. This extra sheet wreaks havoc on soooooooo many processes. I want it gone.

 

I modified the first iteration of code posted above to delete a sheet with a specific name.

 

Dim oSheet As Sheet
Dim oListofSheets As New ArrayList

For Each oSheet In ThisApplication.ActiveDocument.Sheets
oListofSheets.Add(oSheet.Name)
Next

Dim sSheetToDelete As String

sSheetToDelete = "Sheet Name"
oSheet = ThisApplication.ActiveDocument.Sheets.Item(sSheetToDelete) 
oSheet.Delete

 First, I tested the code with a generic sheet name. It worked perfectly:

sSheetToDelete = "Model (AutoCAD)"

Then I substituted in the sheet name I really want to eliminate from all of my drawings:

sSheetToDelete = "Model (AutoCAD)"

This version failed. How do I get inventor to target this sheet?

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Hi @To_Efficiency_and_Beyond.  I almost never work with DWG file type in Inventor, but I know of a couple properties and methods that you may find helpful.

When your code encounters a DrawingDocument object, and you need to test if it is a DWG type, versus an IDW type, you can use the following property to figure that out easily.

DrawingDocument.IsInventorDWG 

When your code encounters a Sheet object, and you want to know if it represents a 'model space' sheet, you can use the following property to check that easily.   You may be able to use that while iterating all Sheets in the DrawingDocument, to find the sheet you want, even if you do not know the exact name of the sheet.

Sheet.IsModelSpaceSheet 

When you have a DrawingDocument, and want to save it as DWG type, you can use the following method.

DrawingDocument.SaveAsInventorDWG 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@To_Efficiency_and_Beyond , from memory, I think the only way to remove Model Space is to save the file as an IDW.

here is a routine to save the file out as a IDW and then save that IDW again as the DWG to strip the Model Space.

 

you can put it in a utility file ( ModelSpace Purger.ipt in the image below) and have the drawing open too, and run the rule from this utility file.

 

 

Curtis_Waguespack_0-1723651150695.png

 

 

Dim oDoc As Document = ThisDoc.Document

Dim oList As New ArrayList

For Each xDoc As Document In ThisApplication.Documents.VisibleDocuments
	If xDoc Is ThisDoc.Document Then Continue For
	If Not xDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then Continue For
	oList.Add(xDoc.FullFileName)
Next

If oList.Count = 0 Then Exit Sub

oTargetDocName = InputListBox("Prompt", oList, oList.Item(0), Title := "iLogic", ListName := "List")

If oTargetDocName = "" Then Exit Sub

Dim oTargetDoc As DrawingDocument = ThisApplication.Documents.ItemByName(oTargetDocName)

hasModelSpace = False
For Each oSheet As Sheet In oTargetDoc.Sheets
	If oSheet.IsModelSpaceSheet Then
		hasModelSpace = True
	End If
Next

If hasModelSpace = True
	sName = oTargetDoc.FullFileName

	sName = Replace(sName, "dwg", "Temp.idw")
	oTargetDoc.SaveAs(sName, True)
	oTargetDoc.Close(True)
	Dim TempDoc As Document = ThisApplication.Documents.Open(sName, True)

	sName = Replace(sName, "Temp.idw", "dwg")
	TempDoc.SaveAsInventorDWG(sName, True)
	TempDoc.Close(True)

	ThisApplication.Documents.Open(sName, True)


End If

 

EESignature

0 Likes