.NET

Reply
Active Member
WangYanbin
Posts: 9
Registered: ‎12-29-2006
Message 1 of 4 (212 Views)

Create,append and save a file.

212 Views, 3 Replies
01-05-2007 10:54 AM
I want create a new dwg file and append an entity(a circle) in it ,but the code excute to the line :
"bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);" ,throw an exception:

Autodesk.AutoCAD.Runtime.Exception: eLockViolation
Autodesk.AutoCAD.Runtime.Interop.ThrowExceptionForErrorStatus(Int32 errorStatus)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager* pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObject(ObjectId id, OpenMode mode, Boolean openErased)

private void button1_Click(object sender, System.EventArgs e)
{
Database db;
DBTransMan tm;
Transaction myT;
BlockTable bt;
BlockTableRecord btr;

Autodesk.AutoCAD.ApplicationServices.Document doc;
doc=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("");

db = doc.Database;
tm = db.TransactionManager;
myT = tm.StartTransaction();
bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);

Circle circle;
circle = new Circle();
circle.Center = new Point3d(100,0,0);
circle.Radius = 300;

btr.AppendEntity(circle);
tm.AddNewlyCreatedDBObject(circle, true);
myT.Commit();

if(bt!=null)
bt.Close();
if(btr!=null)
btr.Close();
if(myT!=null)
myT.Dispose();
doc.CloseAndSave(@"d:\111.dwg");
}

I developed in vs.net 2003 and autocad2006,and this code excute in a ToolPalete.
*Bobby C. Jones
Message 2 of 4 (212 Views)

Re: Create,append and save a file.

01-05-2007 12:06 PM in reply to: WangYanbin
You need to lock your document when accessing it from the Application
Context. Or skip using the document object altogether.

[code]
using System;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace GratuitousNameDisplay.BobbyCJones
{
public class CreateAndAppendToDatabaseExample
{
private const string fileName = @"C:\AddCircle.dwg";

[CommandMethod("CreateCircleDwg")]
public void AddCircle()
{
using (Database targetDb = new Database(true, true))
using (Transaction trans =
targetDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(targetDb.BlockTableId,
OpenMode.ForRead);
BlockTableRecord modelSpace =
(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);

Circle newCircle = new Circle();
newCircle.Center = new Point3d(100, 0, 0);
newCircle.Radius = 300;
newCircle.Normal = new Vector3d(0, 0, 1);

modelSpace.AppendEntity(newCircle);
trans.AddNewlyCreatedDBObject(newCircle, true);

trans.Commit();

targetDb.SaveAs(fileName, DwgVersion.Newest);
}
}
}
}
[/code]

--
Bobby C. Jones

wrote in message news:5442633@discussion.autodesk.com...
I want create a new dwg file and append an entity(a circle) in it ,but the
code excute to the line :
"bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);"
,throw an exception:

Autodesk.AutoCAD.Runtime.Exception: eLockViolation
Autodesk.AutoCAD.Runtime.Interop.ThrowExceptionForErrorStatus(Int32
errorStatus)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObjectInternal(AcDbTransactionManager*
pTM, ObjectId id, OpenMode mode, Boolean openErased, Boolean
forceOpenOnLockedLayer)
Autodesk.AutoCAD.DatabaseServices.TransactionManager.GetObject(ObjectId
id, OpenMode mode, Boolean openErased)

private void button1_Click(object sender, System.EventArgs e)
{
Database db;
DBTransMan tm;
Transaction myT;
BlockTable bt;
BlockTableRecord btr;

Autodesk.AutoCAD.ApplicationServices.Document doc;
doc=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add("");

db = doc.Database;
tm = db.TransactionManager;
myT = tm.StartTransaction();
bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForWrite, false);
btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false);

Circle circle;
circle = new Circle();
circle.Center = new Point3d(100,0,0);
circle.Radius = 300;

btr.AppendEntity(circle);
tm.AddNewlyCreatedDBObject(circle, true);
myT.Commit();

if(bt!=null)
bt.Close();
if(btr!=null)
btr.Close();
if(myT!=null)
myT.Dispose();
doc.CloseAndSave(@"d:\111.dwg");
}

I developed in vs.net 2003 and autocad2006,and this code excute in a
ToolPalete.
Active Member
WangYanbin
Posts: 9
Registered: ‎12-29-2006
Message 3 of 4 (212 Views)

Re: Create,append and save a file.

01-05-2007 09:06 PM in reply to: WangYanbin
Thank you Bobby ,but another problem accured, that is how to create a Database with a template file?
*Bobby C. Jones
Message 4 of 4 (212 Views)

Re: Create,append and save a file.

01-08-2007 06:09 AM in reply to: WangYanbin
You need to create a blank database instead of a default database, compare
the arguments of the database constructor in this code to the previous
example, and then populate it from the desired template.

[code]
using (Database targetDb = new Database(false, true))
{
targetDb.ReadDwgFile(yourTemplateFileName, System.IO.FileShare.ReadWrite,
false, null);
...
}
[/code]
--
Bobby C. Jones

wrote in message news:5443086@discussion.autodesk.com...
Thank you Bobby ,but another problem accured, that is how to create a
Database with a template file?
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.