AutoCAD Map 3D Developer
Welcome to Autodesk’s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to Insert a preexisting AutoCAD drawing into a current drawing as a BLOCK REFERENCE USING C#

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
561 Views, 2 Replies

Unable to Insert a preexisting AutoCAD drawing into a current drawing as a BLOCK REFERENCE USING C#

Hi,

 

I'm trying to programmatically insert a block Reference from a pre-existing drawing into the current drawing.

 

In Block Table ID created.

 

In the current drawing the Block Reference not visible. But while selecting and erasing it shows one entity.

 

And in Block Table the block preview also not shown.

 

Please give me your suggestions and comments.

 

Regards,

Viswanathan Solaisamy

 

Code:

 

public void InsertBlock() // This method can have any name

{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Stores the active document
Editor ed = doc.Editor; //Stores the document's editor
Database dtb = ed.Document.Database; //Stores the database from the editor

Transaction tr = dtb.TransactionManager.StartTransaction(); //Start a transaction with the document's database
DocumentLock docLock = doc.LockDocument();

using (tr)
using (docLock)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(dtb.CurrentSpaceId, OpenMode.ForWrite); //Opens the block table record so you can write to it
BlockTableRecord newBlockDef = new BlockTableRecord(); //Creates a new record in the block table
BlockTable blockTable = (BlockTable)tr.GetObject(dtb.BlockTableId, OpenMode.ForWrite); //Opens the block table so it can be written to
//Pointing new block to correct drawing file
newBlockDef.Name = "Handhole";
newBlockDef.PathName = "D:\\Test\\Data\\Blocks\\Handhole.dwg";
blockTable.Add(newBlockDef); //Adds the block table record to the block table
BlockReference newBlock = new BlockReference(new Point3d(0, 0, 0), newBlockDef.ObjectId); //Insert a block reference with the newly created block
btr.AppendEntity(newBlock); //Inserts the block into the current space (Model or Paper) via the block table record
//Updates the Transaction with all new database objects
tr.AddNewlyCreatedDBObject(newBlockDef, true);
tr.AddNewlyCreatedDBObject(newBlock, true);
tr.Commit(); //Applies all changes made as part of the current transaction
tr.Dispose();
}

}

 

InsertBlock.jpg

 

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

So, you want to insert a block from a block DWG file.

 

Your code is logically wrong:

1. You DO NOT create a new BlockTableRecord to insert a existing block drawing file as block; Instead, you insert the block DWG file as a database into current drawing's database, upon this insertion, the block DWG database becomes a BlockTableRecord (block definition) in current drawing. Then you can create BlockReference based on it;

 

2. Yes, you can create a new BlockTableRecord in current drawing, if necessary, just as user can create block definition manually. However, specifying its "PathName" property does not makes it to grab the external DWG file into the drawing as BlockTableRecord. The "PathName" property IS ONLY USED for Xref. If he BlockTableRecord is not anXref, PathName property holding whatever value does not have effect to the BlockTableRecord at all.

 

Following is quick, simplified code of inserting block DWG file as block definition and then create a block reference based on it (not tested):

 

private ObjectId InsertBlockDefinition(Database thisDB, string fullBlockPathFileName)

{

    var blkId=ObjectId.Null

    var blkName=System.IO.Path.GetFielNameWithoutExtension(fullBlockPathFileName);

 

    // You can check if the block definition with the same name already exists in dwg or not

    // if yes, you just return its Id without inserting the block dwg file; or you can still insert

    // the block DWG file anyway, so that the existing block definition is overwritten silently

 

    using (var db=new Database(false, true))

    {

        db.ReadFromDwgFile(fullBlockPathFileName,....);

        blkId = thisDB.Insert(blkName, db, false)'

    }

    return blkId

}

 

Private void InsertBlock(string blockFileName)

{

    var dwg=....MdiActiveDocuement;

    using (dwg.LockDocument)

    {

        ObjectId blkDefinitionId=InsertBlockDefinition(dwg.Database, blockFileName);

        using (var tran = dwg.TransactionManager.StartTransaction())

        {

             var blkRef=new BlockReference([point], blkDefinitionId);

             ....

             // Add to current space (model or paper)...

             // Add to transaction...

            tran.Commit();

        }

    }

}

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

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

Hi,

 

Thanks for your help.

 

public ObjectId InsertBlockDefinition(Database thisDB, string fullBlockPathFileName)

{
Document acaddoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acaddatabase = acaddoc.Database;
Editor acadeditor = acaddoc.Editor;

//Transaction tr = acaddoc.TransactionManager.StartTransaction(); //Start a transaction with the document's database
DocumentLock docLock = acaddoc.LockDocument();

var blkId = ObjectId.Null;
var blkName = System.IO.Path.GetFileNameWithoutExtension(fullBlockPathFileName);

 

using (docLock)

{
using (var db = new Database(false, true))

{

db.ReadDwgFile(fullBlockPathFileName, FileOpenMode.OpenForReadAndReadShare, false, null);

blkId = thisDB.Insert(blkName, db, false);


}

return blkId;

}

 

}


public void createblockfromDrawing()
{
Document acaddoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acaddatabase = acaddoc.Database;
Editor acadeditor = acaddoc.Editor;

string fullBlockPathFileName = "P:\\Test\\Data\\Blocks\\Handhole.dwg";
var blkId = InsertBlockDefinition(acaddatabase, fullBlockPathFileName);

}

 

Working Fine.

 

Regards,

Viswanathan Solaisamy

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

Post to forums  

Autodesk Design & Make Report