Community
I am trying to make a program that read a dwg and create a BlockReference and place it in a new dwg in batch mode.
My code is as below:
public void processDWG(string fileName) { Autodesk.AutoCAD.ApplicationServices.Document SourceDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(fileName,false,""); ShowWindow(SourceDoc.Window.Handle, 3); //To Maximize the document window. SetForegroundWindow(SourceDoc.Window.Handle.ToInt32()); //To make the document window active. Autodesk.AutoCAD.DatabaseServices.Database Sdb = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database; Autodesk.AutoCAD.EditorInput.Editor Sed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ObjectId[] objIds; Autodesk.AutoCAD.EditorInput.PromptSelectionResult res = Sed.SelectAll(); if(res.Status == PromptStatus.Error) //ALWAYS GETTING Status=ERROR HERE... { MessageBox.Show("Error Getting Entities."); return; } SelectionSet SelSet = res.Value; objIds = SelSet.GetObjectIds(); DBObjectCollection ents = new DBObjectCollection(); using(Transaction txn = Sdb.TransactionManager.StartTransaction()) { foreach(ObjectId oid in objIds) { Entity ent = (Entity)txn.GetObject(oid,OpenMode.ForRead,false); ents.Add(ent); } } Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("acad.dwt"); ShowWindow(doc.Window.Handle, 3); SetForegroundWindow(doc.Window.Handle.ToInt32()); System.Threading.Thread.Sleep(500); Autodesk.AutoCAD.DatabaseServices.Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(doc.Database).Database; Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(doc.Database).Editor; using(Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord blkRec = new BlockTableRecord(); blkRec.Name = "Plot"; bt.UpgradeOpen(); ObjectId blkId = bt.Add(blkRec); trans.AddNewlyCreatedDBObject(blkRec, true); foreach(Entity ent in ents) { Entity ClonedEnt = (Entity)ent.Clone(); ClonedEnt.Color = ent.Color; ClonedEnt.LineWeight = ent.LineWeight; blkRec.AppendEntity(ClonedEnt); trans.AddNewlyCreatedDBObject(ClonedEnt,true); } trans.Commit(); BlockTableRecord ms = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockReference br = new BlockReference(new Point3d(0,0,0),blkId); ms.AppendEntity(br); trans.AddNewlyCreatedDBObject(br,true); trans.Commit(); } //SourceDoc.CloseAndDiscard(); //MessageBox.Show(doc.Name); //object obj = Type.Missing; //((AcadDocument)doc.AcadDocument).SaveAs(outputPath + "\\" + System.IO.Path.GetFileName(fileName), obj, obj); doc.CloseAndSave(outputPath + "\\" + System.IO.Path.GetFileName(fileName)); }
When I am getting the doc from MDIActiveDocument, then it works fine.
But my requirement is to open and process dwg files in Batch Mode.
Anybody ...Please Help..
Thanks..
Chandan.
Hi Chandan,
I didn't try to debug your code, but I feel, you might get a clue by looking into the source code of "ScriptPro 2.0 for AutoCAD" an utility available for download from Autodesk Labs (http://labs.autodesk.com/utilities/ADN_plugins/catalog/ )
Thanks,
Partha Sarkar
Autodesk Developer Network