- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone.
Now I am exploring how entities can be drawn in a document that is not active, but is open. While the drawn entities are recorded to the document's database, they are not keen on being displayed when I make the document active. Sometimes Z+A (zoom all) triggers redrawing the elements, sometimes not. Closing and reopening the document helps though. However, I am looking for the way to have them displayed immediately after they're drawn.
If you would like to help me figure this one out, here is the script I am testing this with:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using Autodesk.AutoCAD.Geometry;
using System.IO;
[assembly: CommandClass(typeof(CPerformTest.PerformTest))]
namespace CPerformTest {
public class PerformTest : IExtensionApplication {
[CommandMethod("performtest")]
public static void Perform() {
try {
string helpFileName = "HelpDrawing.dwg";
Document mainDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor mainEditor = mainDoc.Editor;
mainEditor.WriteMessage("\nPress ENTER to add a new entity and ESC to break the loop.\n");
PromptPointOptions opts = new PromptPointOptions("");
opts.AllowNone = true;
while(true) {
PromptPointResult ppr = mainEditor.GetPoint(opts);
if(ppr.Status == PromptStatus.OK) {
// Just ignore it
} else if(ppr.Status == PromptStatus.None) {
Document helpDrawing = OpenBkgDocument(helpFileName);
if(helpDrawing != null) {
helpFileName = helpDrawing.Name;
Database helpDB = helpDrawing.Database;
using(DocumentLock dl = helpDrawing.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true)) {
using(Transaction tr = helpDB.TransactionManager.StartTransaction()) {
BlockTableRecord btr = (BlockTableRecord) tr.GetObject(helpDB.CurrentSpaceId, OpenMode.ForWrite);
Random r = new Random();
Point3d a = new Point3d(r.NextDouble() * 10, 0, 0);
Point3d b = new Point3d(0, r.NextDouble() * 10, 0);
Line line = new Line();
line.StartPoint = a;
line.EndPoint = b;
ObjectId objId = btr.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
tr.Commit();
}
}
}
} else if(ppr.Status == PromptStatus.Cancel) {
mainEditor.WriteMessage("*Canceled*");
break;
}
}
} catch(System.Exception e) {
}
}
public static Document OpenBkgDocument(string fileName) {
DocumentCollection docs = Application.DocumentManager;
Document result = null;
foreach(Document doc in docs) {
string dname = doc.Name;
if(dname.Equals(fileName, StringComparison.CurrentCultureIgnoreCase)) {
result = doc;
break;
}
}
if(result == null) {
if(!File.Exists(fileName)) {
Document doc = Application.DocumentManager.Add("acad.dwt");
doc.Database.SaveAs(
fileName,
true,
DwgVersion.Current,
doc.Database.SecurityParameters
);
doc.CloseAndDiscard();
}
}
if(result == null) {
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
if(File.Exists(fileName)) {
result = acDocMgr.Open(fileName, false);
}
}
return result;
}
public void Initialize() {
}
public void Terminate() {
}
}
}I use GetPoint to capture enters and escs. Here each ENTER will produce a line in the background drawing, and the ESC will break the loop. Because I am still learning the code might not be optimal in all aspects.
Well, thanks.
Solved! Go to Solution.

