.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I have a program where I am inserting blocks at specific points in model space. I have successfully been able to create a subroutine that will accept a block name, a point3D Location and a layer name and and insert the specified block at the specified location on the specified layer. What I need to do now is to cycle through model space and find all of the pipe objects in model space. I then find the endpoint of each pipe and insert a block at that location. The problem that I am having is that the program crashes when I try to find the objectid of modelspace. I am using the technique discussed in this thread by Jeffrey_H.
Below is the code that I am trying to get to work.
'Test Command to insert the some blocks into the drawing
<CommandMethod("TrimbleTestBlocks")> _
Public Sub TrimbleTestBlocks()
'Get the editor object
Dim doc = AcApp.DocumentManager.MdiActiveDocument
Dim db = doc.Database
Dim ed = doc.Editor
Try
'Get Model Space Block Table Record
Dim mdlspace As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).Get Object(OpenMode.ForRead)
'Start the transaction
Using tm As Transaction = db.TransactionManager.StartTransaction
'Cycle through each object in modelspace
For Each objid As ObjectId In mdlspace
'If the object is a Pipe Object then add a block to the end of it
If objid.ObjectClass.Name = "AecdbPipe" Then
'At this point we know the object is a pipe so create a reference to it.
Dim mpipe As Pipe = tm.GetObject(objid, OpenMode.ForRead, True)
'Get the endpoint of the pipe
Dim PipeEndpoint As Point3d = mpipe.EndPoint
'Insert the block at the endpoint of the pipe
TrimbleInsertBlockAtPoint(INS250, PipeEndpoint, HYD250)
End If
Next
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
'TrimbleInsertBlockAtPoint(INS250, New Point3D(0, 0, 0), HYD250)
'TrimbleInsertBlockAtPoint(INS375, New Point3d(0, 10, 0), HYD375)
'TrimbleInsertBlockAtPoint(INS500, New Point3d(0, 20, 0), HYD500)
'TrimbleInsertBlockAtPoint(INS625, New Point3d(0, 30, 0), HYD625)
'TrimbleInsertBlockAtPoint(INS750, New Point3d(0, 40, 0), HYD750)
'TrimbleInsertBlockAtPoint(INS875, New Point3d(0, 50, 0), HYD875)
'TrimbleInsertBlockAtPoint(INS875, New Point3d(50, 50, 50), HYD875)
End Sub
This is the line that crashes on me
'Get Model Space Block Table Record
Dim mdlspace As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).Get Object(OpenMode.ForRead)
Does anyone have any ideas on why this is crashing the program? I just started programming dotnet this weekend and my head is starting to explode.
My end goal is to cycle through every pipe in model space. If the pipe belongs to a specified system definition then find the pipe size and insert a corresponding block on the endpoint of the pipe. I then want to export the blocks to a new drawing to be imported into a trimble unit for locating inserts on a deck.
Thanks for any help in advance.
Solved! Go to Solution.
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi,
I don't think that the SymbolUtilityServices.GetBlockModelSpaceId() function is the officially recommended way to obtain model space.
Check out the following link from the AutoCAD .NET Developer's Guide:
http://exchange.autodesk.com/autocad/enu/online-he
I think this is what you are looking for. It gives you the recommended method for iterating entities in model space with samples.
Hope this helps.
Art
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
You are trying to add blocks into model space when it is only opened for read. Change it to " OpenMode.ForWrite"
HTH
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I found the problem. The line that was causing the crash was outside the umbrella of the transaction. As soon as I moved it inside the transaction block of code it stopped crashing but I still have some logic errors.
Brent, I am taking care of opening model space for write inside of the subroutine that adds the block.
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I had also forgotten to commit the transaction when I was finished iterating through all of the pipes in a drawing. The code now works as written but I am having a problem figuring out how to find the pipe system definition, routing preference, and the pipe size. I need that information to figure out which block to add to the end of the pipe. Does anyone know how to access this information? They are not properties of the pipe object.
Below is the code that worked as expected.
'Test Command to insert the some blocks into the drawing
<CommandMethod("TrimbleTestBlocks")> _
Public Sub TrimbleTestBlocks()
'Get the editor object
Dim doc = AcApp.DocumentManager.MdiActiveDocument
Dim db = doc.Database
Dim ed = doc.Editor
Try
'Start the transaction
Using tm As Transaction = db.TransactionManager.StartTransaction
'Get Model Space Block Table Record
Dim mdlspace As BlockTableRecord = SymbolUtilityServices.GetBlockModelSpaceId(db).Get Object(OpenMode.ForRead)
'Cycle through each object in modelspace
For Each objid As ObjectId In mdlspace
'If the object is a Pipe Object then add a block to the end of it
If objid.ObjectClass.Name = "AecbDbPipe" Then
'At this point we know the object is a pipe so create a reference to it.
Dim mpipe As Pipe = tm.GetObject(objid, OpenMode.ForRead, True)
'Get the endpoint of the pipe
Dim PipeEndpoint As Point3d = mpipe.EndPoint
'Insert the block at the endpoint of the pipe
TrimbleInsertBlockAtPoint(INS250, PipeEndpoint, HYD250)
'Now that we have added a block to a pipe, write a message to the command line
ed.WriteMessage("Block added at " & PipeEndpoint.ToString & vbLf)
End If
Next
'Commit the transaction
tm.Commit()
End Using
'Catch any exceptions and generate a generic message to the command line
Catch ex As Exception
ed.WriteMessage("There was an error adding the blocks to the drawing..." & vbLf)
End Try
End Sub
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Just a thought - I haven't dealt with it much, but have you tried attaching XData to your pipes/blocks then accessing it as needed?
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I guess I should have mentioned that I was using Autocad MEP. So the pipes are already in the drawing and they already have the information attached to them. I just have no idea on how to read it. There is not alot of information on the API for Autocad MEP or Autocad Architectural so I am fumbling around alot trying to figure out how to do things. So far everything I have done is a pretty straight forward method and is outlined in the Autocad API documentation. I hit the wall once I came to trying to read the information attached to the piping objects.
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I'm assuming you are using AutoCAD MEP versions 2009 thru 2012?, in the MEP program installation folder, you can find quite a few sample NET projects for MEP under the 'Sample\VB.NET' folder or 'Sample\CS.NET' folder. Be sure to check out the 'ReadMe.txt' files for each project. You should be able to find a sample that shows how to get to the info you want.
Maybe check out the code in the samples:
MepUtilities (has a function 'GetPartSizeName()' in the DataUtilities.vb code file)
ExportToExcel
GetEngineeringData
SystemDefs
If you can't find the sample files in your program folder there are zip files attached of the samples for 2010, 2011, & 2012.
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Thanks for the information cadMeUp. I did look through those but I will go through them again and see if i missed anything. The system definations examples show how to print out all of the system defintions in the drawing but unless I am missing something, they dont show how to find out what the system defintion is for an object in the drawing. The getpartsizename() has to do with quering a catalog but maybe I can use the same method to find the information about the part. If I can establish which catalog the part came from then query the catalog with the part guid, i might be able to get the desired information. It just seems that it is going the long way around to find the information that should already be in the database. Using that method, what would happen if the catalog no longer exists? or isnt loaded?
I think that some of the answer lies in the excel example. They are accessing some of the information that is attached to the part but not the system definition or the routing preference. I think I can grab the pipe size from this information though. Thanks again for reminding me to look here. Now I just need to roll up my sleeves and figure out what they are doing.
Re: Program crashes when trying to access modelspace
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Have a look at the attached code file for VS 2008 C#, MEP 2012...



