.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi!
I want to open dwg from template:
DocumentCollection acDocMgr = Application.DocumentManager;
Document acDoc;
try
{
acDoc = acDocMgr.Add(FileNameTemplate);
acDocMgr.MdiActiveDocument = acDoc
}
catch
{
}
But on the line "acDocMgr.MdiActiveDocument = acDoc" AutoCAD is hanging and in around 10 sec AutoCAD closes.
How does the new document from template set active (do current)?
Solved! Go to Solution.
Re: create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
You have to use CommandFlags.Seesion and also
use LockDocument
Here is a quick sample:
[CommandMethod("opdwt", CommandFlags.Session)]
public void testOpenFromTemplate()
{
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D ocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
try
{
using (DocumentLock doclock = doc.LockDocument())
{
string tmpPath = "acad.dwt";
//change file name to save:
string fn = @"C:\Test\FromTemplate.dwg";
Document newdoc = dm.Add(tmpPath);
Database newdb = newdoc.Database;
using (DocumentLock newdoclock = newdoc.LockDocument())
{
using (Transaction newtr = newdoc.TransactionManager.StartTransaction())
{
dm.MdiActiveDocument = newdoc;
Editor newed = newdoc.Editor;
Point3d pt = newed.GetPoint("\nPick point: ").Value;
Line ln = new Line(pt, db.Extmax);
ln.ColorIndex = 1;
BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(newdb.CurrentSpa ceId, OpenMode.ForWrite);
newbtr.AppendEntity(ln);
newtr.AddNewlyCreatedDBObject(ln, true);
newtr.Commit();
MessageBox.Show("Saved as: " + fn);
}
}
newdoc.CloseAndSave(fn);
// just to work further if you wanted be:
dm.Open(fn, false);
MessageBox.Show("You can try to do your rest job here");
}
doc.CloseAndDiscard();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
finally
{
// do nothing
}
}
C6309D9E0751D165D0934D0621DFF27919
Re: create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Yeah, it works good. Thanks. It is done this way:
[CommandMethod("calc", CommandFlags.Session)]
public void doIt()
{
openFromTemplate();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D
// my calculation
}
[CommandMethod("opdwt", CommandFlags.Session)]
public void openFromTemplate()
{
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.D
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.D
try
{
using (DocumentLock doclock = doc.LockDocument())
{
string tmpPath = "my template file dwt";
Document newdoc = dm.Add(tmpPath);
Database newdb = newdoc.Database;
using (DocumentLock newdoclock = newdoc.LockDocument())
{
using (Transaction newtr = newdoc.TransactionManager.StartTransaction())
{
dm.MdiActiveDocument = newdoc;
}
}
}
}
catch {}
}
Re: create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Glad you get it worked, just one note though:
Don't use empty code block for CATCH statement,
better yet to display an error message in case you
have got it,
Cheers ![]()
C6309D9E0751D165D0934D0621DFF27919
Re: create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
But it's not work for one situation.
In main function i call two methods - 1. load dwg through method readdwgfile 2. create drawing from template
In order to the second func works i add this expression above the main func
[CommandMethod("testSituation", CommandFlags.Session)]
But now the first method (readDwgFile) give me error - eNotOpenForWrite. If i remove this expression, the first method can work and the second won't work.
Test code:
[CommandMethod("testSituation", CommandFlags.Session)]
public void testSit()
{
Database database = loadDWG(@"C:\...\myfile.dwg");
openDrawingFromTemplate(@"\\tn...\A2_P.dwt");
}public Database loadDWG(string PathDWG)
{
Database newDWG = new Database(false, true);
if (System.IO.File.Exists(PathDWG))
{
try
{
newDWG.ReadDwgFile(PathDWG, FileOpenMode.OpenTryForReadShare, true, "");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error");
}
return newDWG;
}
else
{
System.Windows.Forms.MessageBox.Show(String.Format ("File {0} is not exist", PathDWG), "Error");
return null;
}
}
Is such way exist which can help to solve this situation?
Re: create drawing from template dwt
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
May be there are such flags which can help to work both methods?
