• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Valued Contributor
    santoshr0114
    Posts: 59
    Registered: ‎03-21-2012
    Accepted Solution

    Insert Block From another File

    439 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

    Santosh Ramamurthy
    Software Engineer
    MicroGenesis Consulting Services Pvt. Ltd.
    Please use plain text.
    Valued Contributor
    Posts: 84
    Registered: ‎03-19-2007

    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. 

    Please use plain text.
    Valued Contributor
    santoshr0114
    Posts: 59
    Registered: ‎03-21-2012

    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

    Santosh Ramamurthy
    Software Engineer
    MicroGenesis Consulting Services Pvt. Ltd.
    Please use plain text.
    Valued Contributor
    santoshr0114
    Posts: 59
    Registered: ‎03-21-2012

    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

    Santosh Ramamurthy
    Software Engineer
    MicroGenesis Consulting Services Pvt. Ltd.
    Please use plain text.
    New Member
    Posts: 2
    Registered: ‎10-01-2007

    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.

    Please use plain text.
    Valued Contributor
    santoshr0114
    Posts: 59
    Registered: ‎03-21-2012

    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();
    }
    }

    Santosh Ramamurthy
    Software Engineer
    MicroGenesis Consulting Services Pvt. Ltd.
    Please use plain text.