.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create,append and save a file.

3 REPLIES 3
Reply
Message 1 of 4
WangYanbin
486 Views, 3 Replies

Create,append and save a file.

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.
3 REPLIES 3
Message 2 of 4
Anonymous
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.
Message 3 of 4
WangYanbin
in reply to: WangYanbin

Thank you Bobby ,but another problem accured, that is how to create a Database with a template file?
Message 4 of 4
Anonymous
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?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost