Problems deleting TitleBlock in sheet

Problems deleting TitleBlock in sheet

mowag
Enthusiast Enthusiast
429 Views
10 Replies
Message 1 of 11

Problems deleting TitleBlock in sheet

mowag
Enthusiast
Enthusiast

Hi,

 

Trying to delete all borderdefinitions and titleblockdefinitions of a drawing.

 

Before deleting the definitions (border or titleblock) i check if they are referenced. If so i iterate over all the sheets in the drawing to delete them. The i  can delete the definition (border or titleblock).

 

This works for the borders, but not for the titleblocks.

The osheet.TitleBlock.delete() gives a 'Object reference not set to an instance of an object' error !

 

                foreach (Inventor.TitleBlockDefinition titleblockDefinition in oDrawDoc.TitleBlockDefinitions)
                {
                    if (titleblockDefinition.Name.Contains("Iso-Mecotech"))
                    {
                        if (titleblockDefinition.IsReferenced)
                        {
                            foreach (Inventor.Sheet osheet in oDrawDoc.Sheets)
                            {
                                if (osheet.TitleBlock.Name == titleblockDefinition.Name)
                                {
                                    osheet.TitleBlock.Delete();
                                }
                            }
                        }
                        titleblockDefinition.Delete();
                    }
                }

 

I used the same code for the Borders without any problem 

 

                foreach (Inventor.BorderDefinition originalborderdef in oDrawDoc.BorderDefinitions)
                {
                    if (originalborderdef.Name.Contains("Watermerk"))
                    {
                        if (originalborderdef.IsReferenced)
                        {
                            foreach (Inventor.Sheet sheet in oDrawDoc.Sheets)
                            {
                                if (sheet.Border.Name == originalborderdef.Name)
                                {
                                    sheet.Border.Delete();
                                }
                            }
                        }
                        originalborderdef.Delete();
                    }
                }

 

 

0 Likes
Accepted solutions (1)
430 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

It is possible for the Sheet.TitleBlock property to return 'Nothing', so is it possible that the code is encountering a Sheet which does not have a TitleBlock on it?  If so, it would throw an error when your line of code tries to access the property or method of the TitleBlock object that does not exist.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 11

mowag
Enthusiast
Enthusiast

Hi WCrihfield,

 

Yes, you're right, it could return null.

But in the line above the delete() statement i'm already checking the Name of the TitleBlock.

This is the cleaned up version of my code . Just after the TitleBlock.Name comparison i did a log entry with the Name of the titleblock.

This line was in the log file, so the TitleBlock could hardly return null, i think.

 

But i'll do the test anyway !!

 

 

0 Likes
Message 4 of 11

JelteDeJong
Mentor
Mentor

I have read your code but I still do not fully understand what the goal is. The nested if statements and loops make the logic difficult to follow. I expect that the same functionality can be written in a simpler way, which would also make debugging easier.
Can you clarify what you want to clean up.
Are you trying to clean up the drawing resources. Do you want to remove all title block definitions that do not contain the text Watermerk in their name.
Or
Are you trying to remove all title blocks from all sheets where the title block name contain the text Watermerk in the name.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 11

mowag
Enthusiast
Enthusiast

 

the goal is to strip a drawing from all of its Drawing Resources.

The company changed it's name and wanted a watermark on the Drawings.

So i wrote a C# app such that i batch process al the drawing files in a folder.

At least Borderdefenitions that contains "Watermerk" in the name and Titleblocks that contains '"Iso-Mecotech" in the name.

As WCrihfield pointed out : before deleting a definition in the Drawing Resources you have to check if the definition is refereced somewhere. If so you have to iterate over all the sheets in the drawing, deleting there the 'instance' of that definition, and then you can delete that Definition in the Drawing Resources.

I excetute the code for the Borderdefinitions, and this goes well. When i excecute the code for the TitleDefinitions, i got an error 'Object reference not set to an instance of an object'  at the statement osheet.TitleBlock.delete().

 

Then i start copying (from a 'Standard Reference drawing) the new Borderdefinitions in the Drawing (10, for each Size (A0-A4,landscape and portrait) and also the new TitleBlock defintion.

Set the appropriate BorderDefinition accordingly to the Size an Orientation of the drawing + setting the new TtileBlockdefinitiion

At last, i copy the Sketched Symbols that are not yet in the drawing.

 

0 Likes
Message 6 of 11

JelteDeJong
Mentor
Mentor

You could try something liek this:

// Loop over all sheets
foreach (Inventor.Sheet sheet in oDrawDoc.Sheets)
{
    // If title block definition name contains
    // "Iso-Mecotech" then continue to the next sheet
    if (sheet.TitleBlock.Definition.Name.Contains("Iso-Mecotech")) continue;

    // Delete the titleblock
    sheet.TitleBlock.Delete();
}

// We now only have sheets left with title blocks that we want.
// We can now loop over all title block definitions
foreach (Inventor.TitleBlockDefinition titleblockDefinition in oDrawDoc.TitleBlockDefinitions)
{
    // if the definition is refrenced  then we continiue to the next definition
    if (titleblockDefinition.IsReferenced) continue;

    // otherewise we delete the title block definition.
    titleblockDefinition.Delete();
}

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 7 of 11

mowag
Enthusiast
Enthusiast

@JelteDeJong 

 

i 've tried your approach in separate loops, but result is the same ........ error at sheet.TitleBlock.delete().

 

@WCrihfield 

 

i checked also  for null .... to be 100% sure !

0 Likes
Message 8 of 11

WCrihfield
Mentor
Mentor

I do not write code in C#.net so I do not fully understand all of its intricacies, but do understand it (to a certain degree) due to its similarities to vb.net.  Since the main issue seems to be with deleting TitleBlocks in an effort to delete (and potentially replace) a TitleBlockDefinition, I decided to write a vb.net version of a custom Function for a task like this within the iLogic rule editor.  As you can see, I often prefer to use inverse checks to 'skip' entities I am not interested in processing further, to shorten overall codes, and potentially make them easier to read. This version is interlaced with Try...Catch...End Try statements at every opportunity for a possible failure, and include iLogic 'Logger' entries on their 'Catch' side, to hopefully provide a high level of feedback to the iLogic Log window.  But those 'Logger' entries could be replaced by whatever other form of feedback you may prefer, to make it compatible for add-in or standalone exe use.  Not sure if this will help any, but here it is anyways, just as additional reference material for everyone trying to help out here, but maybe not that familiar with C#.net.

Public Function DeleteTitleBlockDefinition(ByRef drawingDoc As Inventor.DrawingDocument, _
	ByVal TitleBlockDefinitionName As String, _
	ByVal Optional DeleteSheetTitleBlocksReferencingIt As Boolean = False, _
	ByVal Optional DeleteSheetFormatsReferencingIt As Boolean = False) As Boolean
	Dim bDeleted As Boolean = False 'result to return (True = deleted)
	If Not drawingDoc.IsModifiable Then Return False
	For Each oTBDef As Inventor.TitleBlockDefinition In drawingDoc.TitleBlockDefinitions
		If Not oTBDef.Name.Contains(TitleBlockDefinitionName) Then Continue For
		If oTBDef.IsReferenced Then
			If DeleteSheetTitleBlocksReferencingIt Then
				'check each Sheet, delete each TitleBlock referencing this TitleBlockDefinition
				For Each oSheet As Inventor.Sheet In drawingDoc.Sheets
					If oSheet.TitleBlock Is Nothing Then Continue For
					If Not oSheet.TitleBlock.Name = oTBDef.Name Then Continue For
					Try
						oSheet.TitleBlock.Delete()
					Catch ex As Exception
						Logger.Error(vbCrLf & _
						"Error Deleting TitleBlock on Sheet:  " & oSheet.Name & vbCrLf & _
						"of following drawing:" & vbCrLf & _
						drawingDoc.FullDocumentName & vbCrLf & _
						ex.ToString())
					End Try
				Next 'oSheet
			End If
			If DeleteSheetFormatsReferencingIt Then
				'check each SheetFormat, delete any referencing this TitleBlockDefinition
				For Each oSF As Inventor.SheetFormat In drawingDoc.SheetFormats
					If oSF.ReferencedTitleBlockDefinition Is Nothing Then Continue For
					If Not oSF.ReferencedTitleBlockDefinition Is oTBDef Then Continue For
					Try
						oSF.Delete()
					Catch ex As Exception
						Logger.Error(vbCrLf & _
						"Error Deleting SheetFormat named:  " & oSF.Name & vbCrLf & _
						"from following drawing:" & vbCrLf & _
						drawingDoc.FullDocumentName & vbCrLf & _
						ex.ToString())
					End Try
				Next 'oSF
			End If
			'check again if it is still referenced...if so, then skip it to avoid error
			If oTBDef.IsReferenced Then
				Logger.Warn("TitleBlockDefinition named:  " & oTBDef.Name & vbCrLf & _
				"...is still being referenced after TitleBlock & SheetFormat deletions!")
				Continue For
			End If
			'now try to delete this TitleBlockDefinition
			Try
				oTBDef.Delete()
				bDeleted = True
			Catch ex As Exception
				Logger.Error(vbCrLf & _
				"Error Deleting TitleBlockDefinition named:  " & oTBDef.Name & vbCrLf & _
				"from following drawing:" & vbCrLf & _
				drawingDoc.FullDocumentName & vbCrLf & _
				ex.ToString())
			End Try
		End If
		If bDeleted Then Exit For
	Next 'oTBDef
	Return bDeleted
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 11

mowag
Enthusiast
Enthusiast

Added som more debug code...

Found out that is was trying to delete a TitleblockDefinition that still was referenced.....

I first iterated over all the sheets in the drawing and deleted the TitleBlock of every sheet!

Then i tried to delete all the TitlBlockDefinitions in the drawing.

Left is before the deletion of all the TitleBlocks in the drawing and right when all the TitlBlocks has been deleted.

 

This i want to delete the  ISO Titlblockdefinition and got an error ( see below)

Ther you see that the ISO TitleBlockDefinition is still referenced ???

How could it be referenced when the TitleBlocks in all the sheets of the drawing are deleted ?

 

Screenshot_1.pngScreenshot_2.png

 

Screenshot_3.png

 

0 Likes
Message 10 of 11

WCrihfield
Mentor
Mentor
Accepted solution

I do not know how the definition could still be referenced after all placed instances which referenced it have been deleted.  You already said that you did not have any SheetFormats, which was the other thing I mentioned (SheetFormat.ReferencedTitleBlockDefinition), so I can not think of anything else that could/would be referencing it.  It would be nice if there was a Property or Method to help us figure out 'what' is referencing it.  This may be a situation to share with Autodesk Tech Support, just in case there may actually be some sort of 'bug' involved.  This may sound stupid at this point, but maybe try doing a document update after all place instance deletions, but before attempting to delete the definition.  Just a 'shot in the dark', but a simple update has fixed several 'odd' situations for me in the past.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 11

mowag
Enthusiast
Enthusiast

 

Now that you mentionned again the SheetFormats, i asked the company to look in the Sheet Format folder to see what was in there. Turned out that there were indeed Sheet Formats with the ISO TitleBlock. Although they never made use of it.

So i deleted all the Sheet Formats and was the able to delete the ISO TitleBlock in the Drawing Resources !

Thank you for pointing out the SheetFormats again to me !

0 Likes