.NET

Reply
Mentor
santoshr0114
Posts: 161
Registered: ‎03-21-2012
Message 1 of 6 (1,385 Views)
Accepted Solution

Insert Block From another File

1385 Views, 5 Replies
07-05-2012 02:28 AM

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

Regards
Santosh
Valued Contributor
CommCorp
Posts: 97
Registered: ‎03-19-2007
Message 2 of 6 (1,377 Views)

Re: Insert Block From another File

07-05-2012 04:58 AM in reply to: santoshr0114

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.Database
                        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. 

Mentor
santoshr0114
Posts: 161
Registered: ‎03-21-2012
Message 3 of 6 (1,375 Views)

Re: Insert Block From another File

07-05-2012 05:28 AM in reply to: CommCorp

Hi CommCorp,

 

Will Try and post the result back

 

Thank You for your time

Regards
Santosh
Mentor
santoshr0114
Posts: 161
Registered: ‎03-21-2012
Message 4 of 6 (1,353 Views)

Re: Insert Block From another File

07-05-2012 10:33 PM in reply to: santoshr0114

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

Regards
Santosh
Member
nickmerchant
Posts: 4
Registered: ‎10-01-2007
Message 5 of 6 (1,121 Views)

Re: Insert Block From another File

02-08-2013 02:17 PM in reply to: santoshr0114

Could I see that code? I've been struggling with this concept and have dropped it several times, resorting to Lisp.

Mentor
santoshr0114
Posts: 161
Registered: ‎03-21-2012
Message 6 of 6 (1,106 Views)

Re: Insert Block From another File

02-09-2013 01:47 AM in reply to: nickmerchant

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.Database;
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.ForWrite) as BlockTableRecord;
//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(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
//Our work here is done
myT.Commit();
}
}

Regards
Santosh

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community