Reply
Solved! Go to solution

Delete objects of unknown type from drawing

Hi all,

 

I'm trying to clear all SolidEdgeFramework.DrawingObjects inside a Sheet. The DrawingObjects can be of any type, lets say a Circle2d or BlockOccurence. My goal is to delete all objects in the sheet. I'm not sure how to achieve this, other than trying to cast the returned objects from SolidEdgeFramework.DrawingObjects.Item(i) to all possible object types that could appear in a drawing and then use item.Delete(). Is there a better solution?

 

Regards,

Jonas

9 REPLIES
Solution
Solution
Accepted by topic author freijon
‎08-26-2015 04:32 AM

Re: Delete objects of unknown type from drawing

Re: Delete objects of unknown type from drawing

Thanks Jason for your quick solution! I didn't know about the "dynamic" keyword in C#. That opens a lot of possibilities!

Re: Delete objects of unknown type from drawing

[ Edited ]

You're welcome.

 

The dynamic keyword was added in C# 4.0 (2009-ish). It saves us from having to use GetType().InvokeMember() which was a PIA. Keep in mind that it is using reflection under the hood and can throw an exception if a particular member (like Delete()) does not exist for the object. In cases like this though, we're 99% sure we know what to expect.

 

p.s. Be sure to notice the application.ScreenUpdating stuff I did in the sample. If there are a lot of objects, your code will execute very slow as it's waiting for the UI to update for each Delete().

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Delete objects of unknown type from drawing

Oh bummer... I'm using version 3.5 for my app. I guess I'll have to figure out a way around it then, or try to use 4.0. If I remember correctly, I didn't use 4.x because it doesn't work well with ILMerge. I'll let you know what I came up with.

Re: Delete objects of unknown type from drawing

Don't let ILMerge stop you. I use ILRepack. See the SolidEdgeCommunity/SolidEdge.Community.Reader source code for a working example. In the project, I embed SharpZipLib.

 

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Delete objects of unknown type from drawing

[ Edited ]

I ported my app to 4.0 and ILRepack. Thanks for the hint, also your .target file helped a lot.

I can now focus on the issue of the topic again, which is deleting all objects.

 

I used your code and now get the following error:

 

'System.__ComObject' does not contain a definition for 'Delete'.

 

I then used the Spy for SolidEdge to try to determine what kind of DrawingObject in the particular drawing causes the problem. However, wenn I expand "DrawingObjects" it doesn't show me the list of the Items, even though there should be 18 items.

This causes me to believe that there is something wrong with the DrawingObjects in that drawing, because it works with simple drawings where I manually add a few lines.

Re: Delete objects of unknown type from drawing

Thanks for the heads up about Spy. I have corrected the issue in version 2.4 availble on CodePlex. I had an issue with deployment that I discuessed in Spy for Solid Edge - Important update. Long story short, you'll need to uninstall your current Spy for Solid Edge and reinstall it from the website.

 

Once you get the new version installed and running, let me know what type of object that does not have the Delete() method and I'll continue to help you.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Delete objects of unknown type from drawing

[ Edited ]

Thanks again for your help. Reinstalling Spy for Solid Edge indeed did fix the issue with not seeing the objects.

Once that worked again, I was able to identify the objects causing the problem:

 

 

SolidEdgeDraft.TablePage is a DrawingObject without Delete() method, however its parent has one, so here is the code that fixed my problem:

 

http://paste.ofcode.org/hY2dWcAAXkQLMrQaTqC5aP

 

Maybe this doesn't work for all DrawingObjects out there, but at least for my case it fixed the issue.

Thanks again for your help!

Re: Delete objects of unknown type from drawing

I updated my DeleteAllDrawingObjects.cs on GitHub to show you how to handle specific types that you know do not have a Delete() method but the parent does. This approach is preferred over letting an exception get raised. The reason is performance. Any time you can avoid a .NET exception, you should. They are very expensive and can greatly slow down your code. Here is a snippet.

 

// Loop until count is 0.
while (drawingObjects.Count > 0)
{
	// Leverage dynamic keyword to allow invoking Delete() method.
	dynamic drawingObject = drawingObjects.Item(1);

	if (drawingObject is SolidEdgeDraft.TablePage)
	{
		// TablePage does not have a Delete() method but its parent does.
		dynamic drawingObjectParent = drawingObject.Parent;
		drawingObjectParent.Delete();
	}
	else
	{
		drawingObject.Delete();
	}
}

 

Jason Newell
Applications Architect
www.jasonnewell.net