AutoCAD Map 3D Developer

AutoCAD Map 3D Developer

Reply
Active Member
ckrath
Posts: 8
Registered: ‎02-10-2011
Message 1 of 2 (508 Views)

editor.selectall() Problem...

508 Views, 1 Replies
03-21-2011 08:42 AM

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.

 

ADN Support Specialist
Partha.Sarkar
Posts: 543
Registered: ‎06-09-2009
Message 2 of 2 (487 Views)

Re: editor.selectall() Problem...

03-23-2011 03:20 AM in reply to: ckrath

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



Partha Sarkar
Developer Technical Services
Autodesk Developer Network

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 or visit the Installation and Licensing Forum to get help installing your software.