.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi Guys,
I am inserting a block from another file to the current drawing file.
I am using C#.Net.
My block gets added into the blocktable record of the current file, but while inserting the block using the "Block" command in AutoCAD its shows an error "The Existing Block has not been modified" I have attached a screenshot of the message i am getting.
I want to insert the block automatically to a user specified location.
How to resolve it ?
Thank You
Software Engineer
MicroGenesis Consulting Services Pvt. Ltd.
Solved! Go to Solution.
Re: Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Below is code for how I have added blocks in VB.Net.
Note that in this case I added blocks to paperspace but you can change that easily.
Dim BlkFullPath As String
Dim currDwgDB As Database = myAcadApp.DocumentManager.MdiActiveDocument.Databa se
Try
' Awesome! this seaches the support paths for the file and if found returns the full path with file name.
BlkFullPath = HostApplicationServices.Current.FindFile(BlkName & ".dwg", currDwgDB, FindFileHint.Default)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MessageBox.Show(ex.Message & ": " & BlkName & ".dwg" & " not found in support paths.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End Try
Using acLckDoc As DocumentLock = SheetDoc.LockDocument()
Using trx As Transaction = mySheetDB.TransactionManager.StartTransaction
Dim ObjId As ObjectId
Dim bt As BlockTable = mySheetDB.BlockTableId.GetObject(OpenMode.ForRead)
Dim btrMs As BlockTableRecord = bt(BlockTableRecord.PaperSpace).GetObject(OpenMode .ForWrite)
Using dbInsert As New Database(False, True)
dbInsert.ReadDwgFile(BlkFullPath, IO.FileShare.Read, True, "")
ObjId = mySheetDB.Insert(Path.GetFileNameWithoutExtension( BlkFullPath), dbInsert, True)
End Using
Dim BlkRef As New BlockReference(New Point3d(BlkX, BlkY, 0), ObjId)
btrMs.AppendEntity(BlkRef)
trx.AddNewlyCreatedDBObject(BlkRef, True)
' explode the block
BlkRef.ExplodeToOwnerSpace()
' when using ExplodeToOwnerSpace, if you dont do this then the block is retained in the drawing.
BlkRef.Erase(True)
trx.Commit()
End Using
End Using
.
Note that in this case Im adding to PaperSpace but you can change that.
Re: Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi CommCorp,
Will Try and post the result back
Thank You for your time
Software Engineer
MicroGenesis Consulting Services Pvt. Ltd.
Re: Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi,
I actually was not inserting a block i was just adding it to the blocktable record, thats why i was getting an error,
When i looked at the last segment of your code i remembered what i missed.
I finshed the task sucessfully & just to inform you, i wrote the code in C#
Thank You for the solution
Software Engineer
MicroGenesis Consulting Services Pvt. Ltd.
Re: Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Could I see that code? I've been struggling with this concept and have dropped it several times, resorting to Lisp.
Re: Insert Block From another File
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Find the below code, Alter as per your needs.
Find the code below, Alter as per your needs...
public void AddBlocktoDataBase()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (Database OpenDb = new Database(false, true))
{
PromptStringOptions pStrOpts = new PromptStringOptions("Ask for the template like 1,2,3,4,etc");
pStrOpts.AllowSpaces = false;
PromptResult pStrRes = doc.Editor.GetString(pStrOpts);
if (pStrRes.Status != PromptStatus.OK)
return;
if (pStrRes.StringResult.Equals("1"))
{
OpenDb.ReadDwgFile("Template path here", System.IO.FileShare.ReadWrite, true, "");
blkname = "TPS-A1-Manufacturing-India";
}
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr = OpenDb.TransactionManager.StartTransaction())
{
//For example, Get the block by name "BlkName"
BlockTable bt;
bt = (BlockTable)tr.GetObject(OpenDb.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockkname))
{
ids.Add(bt[blockname]);
}
tr.Commit();
}
//if found, add the block
if (ids.Count != 0)
{
//get the current drawing database
Database destdb = doc.Database;
IdMapping iMap = new IdMapping();
destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Ignore, false);
}
}
}
public void AddBlocktoModelSpace()
{
Database db = Application.DocumentManager.MdiActiveDocument.Data
using (Transaction myT = db.TransactionManager.StartTransaction())
{
//Get the block definition "Check".
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef = bt[blockname].GetObject(OpenMode.ForRead) as BlockTableRecord;
//Also open modelspace - we'll be adding our BlockReference to it
BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode
//Create new BlockReference, and link it to our block definition
using (BlockReference blockRef = new BlockReference(insertionpoint, blockDef.ObjectId))
{
//Add the block reference to modelspace
ms.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
//Iterate block definition to find all non-constant
// AttributeDefinitions
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
//This is a non-constant AttributeDefinition
//Create a new AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
//attRef.TextString = "Santosh";
//Add the AttributeReference to the BlockReference
blockRef.AttributeCollection.AppendAttribute(attRe
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}
Software Engineer
MicroGenesis Consulting Services Pvt. Ltd.

