.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error on Region.CreateFromCurves

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
alan
991 Views, 2 Replies

Error on Region.CreateFromCurves

I'm trying to create a routine that inserts a dwg containing a polyline into the current drawing so that it can be extruded (eventually it will also rotate it, but I haven't got that far yet). I am having difficulty converting the block to a region using the Region.CreateFromCurves method. If I stop the routine before this point the dwg has been inserted into the drawing and exploded to a polyline, so I am guessing that it is a problem adding the block/ployline to the DBObjectCollection. Any ideas would be much appreciated?

 

I have attached a copy of the dwg I am using to import, and calling the sub with the following line:

 

CreateExtrusion(ReferencePathName + "Rafter.dwg", "0", New Point3d(0, 0, 0), 200)

 

 

 

Public Sub CreateExtrusion(FileName As String, LayerName As String, InsertPoint As Point3d, Length As Double)

 

' Get Editor
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor


' Get Database
Dim dwg As Database = ed.Document.Database

 

'Lock document
Using dl As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()

 

'Begin Transaction
Using trans As Transaction = dwg.TransactionManager.StartTransaction()

 

Dim blockRef As BlockReference = InsertFile(FileName, dwg, trans)

 

'check if layer exists/create
CheckLayer(LayerName, trans, dwg)
blockRef.Layer = LayerName

 

'set focus to the editor
Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()

 

'Set Position
blockRef.Position = InsertPoint

 

' add it to the current space, first open the current space for write
Dim btr As BlockTableRecord = trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite, True, True)

 

' Add block reference to current space
btr.AppendEntity(blockRef)


trans.AddNewlyCreatedDBObject(blockRef, True)

 

'Explode
blockRef.ExplodeToOwnerSpace()

 

Dim colregion As DBObjectCollection = New DBObjectCollection
Dim colpoly As DBObjectCollection = New DBObjectCollection

colpoly.Add(blockRef)

 

Dim acregion As New Region

'ERROR AT NEXT LINE!!!!!!!!!!!!!
colregion = Region.CreateFromCurves(colpoly)
acregion = DirectCast(colregion(0), Region)

 

Dim solid As New Solid3d
solid.Extrude(acregion, Length, 0)

 

btr.AppendEntity(solid)
trans.AddNewlyCreatedDBObject(solid, True)

 

'commit the transaction
trans.Commit()

 

End Using

End Using


End Sub

2 REPLIES 2
Message 2 of 3
norman.yuan
in reply to: alan

<QUOTE>
 

'Explode
blockRef.ExplodeToOwnerSpace()
 

Dim colregion As DBObjectCollection = New DBObjectCollection

Dim colpoly As DBObjectCollection = New DBObjectCollection
colpoly.Add(blockRef)
 

Dim acregion As New Region

'ERROR AT NEXT LINE!!!!!!!!!!!!!

colregion = Region.CreateFromCurves(colpoly)

acregion = DirectCast(colregion(0), Region)
 

</QUOTE>
 


The direct reason of the error is that the DBObject in the DBObjectCollection "colpoly" is wrong type. They should be curves, while you added a BlockReference, thus the error.
 
This is because of your misunderstanding of ExplodeToOwnerSpace() method (I have to say that the ObjectARX document on this method is also incomplete.
Here is the quote from the document:
 


BlockReference.ExplodeToOwnerSpace Method 

Explodes the block reference into individual components. It appends the resulting entities to the BlockTableRecord that owns the BlockReference on which this method was called.
 


What It does not make clear is that the BlockReference itself remains unchanged, and the individual components of the block reference are generated and added to the same space as the BlockReference.
 
In your case, since you need to obtain a set of curves/polylines from the block reference to create Region objects, you'd better use Explode() method, instead of ExplodeToOwnerSpace(), because you really do not need to add the polygons to the database. You may also want to erase the BlockReference itself after calling Explode().
 
Following a quick code I tried and worked OK, assuming a block consisting of 2 closed polylines is to be used to create Region objects:

 

[CommandMethod("MyRegion")]
public static void CreateRegionOutOfBlock()
{
    Document dwg = Application.DocumentManager.MdiActiveDocument;
    Editor ed = dwg.Editor;

    ObjectId blkId = SelectBlock(ed);
    if (!blkId.IsNull)
    {      
        using (var tran = dwg.TransactionManager.StartTransaction())
        {
            BlockReference bref = (BlockReference)
                tran.GetObject(blkId, OpenMode.ForRead);

            using (DBObjectCollection dbObjs = new DBObjectCollection())
            {
//Get the individual components from the BlockReference bref.Explode(dbObjs);
//Erase the BlockReference bref.UpgradeOpen(); bref.Erase(true);
//Create Regions DBObjectCollection col = Region.CreateFromCurves(dbObjs);
//Add newly created regions to current space BlockTableRecord space = (BlockTableRecord) tran.GetObject(dwg.Database.CurrentSpaceId, OpenMode.ForWrite); foreach (DBObject obj in col) { space.AppendEntity(obj as Entity); tran.AddNewlyCreatedDBObject(obj, true); }
//Dispose individual block components from the Explode() operation foreach (DBObject o in dbObjs) { o.Dispose(); } } tran.Commit(); } } Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt(); } private static ObjectId SelectBlock(Editor ed) { PromptEntityOptions opt = new PromptEntityOptions( "\nSelect a block:"); opt.SetRejectMessage("\nInvalid selection: not a block"); opt.AddAllowedClass(typeof(BlockReference), true); PromptEntityResult res = ed.GetEntity(opt); if (res.Status == PromptStatus.OK) return res.ObjectId; else return ObjectId.Null; }

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3
alan
in reply to: norman.yuan

Thanks very much. Works great. Thanks for the explanation too, I hadn't considered the explode to be the issue.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost