adding new document and activating it

adding new document and activating it

ravindrathorat
Explorer Explorer
525 Views
2 Replies
Message 1 of 3

adding new document and activating it

ravindrathorat
Explorer
Explorer

Hi all,

 

I am new to autocad customization. I am trying to add the new document into the autocad through the programming but only first docuement is remains active.  The command passed to new document is taken by  first document.

 

how I can procede to this.

0 Likes
526 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

You need to show your "programming" code for anyone to suggest anything meaningful without guess.

 

With that said, assuming that you are using AutoCAD .NET API, let me play the guess game:

 

If you add/open a document in your custom command (CommandMethod) without CommandFlags.Session flag, the newly added/opened document will not become MdiActiveDocument. That is, you need to set CommandFlags.Session flag in CommandMethod attribute.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

ravindrathorat
Explorer
Explorer

in below mentioned code I am trying to add new Document and in Document trying to add few blocks. blocks are not getting added. i Have tried with lock document method as well.

if i am using acDocMgr.MdiActiveDocument = acDoc; this line code gets failed saying internal error !dbinsert.cpp@836:enoDatabase

 

and if not using the acDocMgr.MdiActiveDocument = acDoc; then blocks getting added , but on first document not on new added document.

 

please help...

 

 

 

 

private void btnCreate_Click(object sender, EventArgs e)
        {
                      
            DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;

            Document acDoc = acDocMgr.Add("acad.dwt");

            acDocMgr.MdiActiveDocument = acDoc;
            
            string filepath = "BlockCollection Path";

 

            Point3d ip1 = new Point3d(6118.1031, 17854.7671, 0);
            Point3d ip2 = new Point3d(6118.1031 + 41.6738, 17854.7671 - 70.5439, 0);

             Point3d ip3 = new Point3d(6118.1031 + 41.6738, 17854.7671 - 70.5439 - 24.6172, 0);
            Point3d[] mPoints= { ip1, ip2, ip3};
          

         for (int i = 0; i < mPoints.Count; i++)
            {
                try
                {
                    string blkFilepath = filepath + chkCollection[i];

                    InsertBlock(blkFilepath, mPoints[i], acDoc);//acDoc

                    blkFilepath = string.Empty;                  
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

        }

 

    public void InsertBlock(string dwgName, Point3d inspt, Document acDoc) //
        {
            Transaction tr = acDoc.TransactionManager.StartTransaction();

            try
            {
                Database db = new Database(false, true);
             
                db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, "");
            
                ObjectId BlkId;
           
                BlkId = acDoc.Database.Insert(System.IO.Path.GetFileNameWithoutExtension(dwgName), db, false);
         
                BlockTable bt = (BlockTable)tr.GetObject(acDoc.Database.BlockTableId, OpenMode.ForRead, true);
               
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
               
                BlockReference bref = new BlockReference(inspt, BlkId);
               
                btr.AppendEntity(bref);
                
                tr.AddNewlyCreatedDBObject(bref, true);
               
                tr.Commit();
               
                tr.Dispose();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                tr.Commit();
                tr.Dispose();
            }
        

        }

0 Likes