C# AutoCAD drawing file write protection errors in Batch loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
The following batch loop will work for as far as pulling up a group of drawings and saving them
when the sample routine "public static void AddCircle()" is flagged out. When I try to make drawing
changes using routine public static void AddCircle() the DrawingSaved routine give me drawing write
protection errors. How can I avoid drawing write protections errors in my batch loop.
//CODE
private void button5_Click(object sender, EventArgs e)
{
//Loop through each item in the array
//foreach (string myStr in thisFormsArray)
BatchLoop();
}
//Batch loop to open close drawings
public void BatchLoop()
{
foreach (String myStr in thisFormsArray)
{
MessageBox.Show(myStr);
// Open the Drawing
strFileName = myStr;
OpenDrawing();
//Add Drawing Changes code here.
AddCircle();// My Sample Routine
DrawingSaved();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//Close drawing
doc.CloseAndDiscard();
}
}
public static void DrawingSaved()
{
//Savedwg
object obj = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DBMOD");
// Check the value of DBMOD, if 0 then the drawing has no unsaved changes
if (System.Convert.ToInt16(obj) != 0)
{
if (System.Windows.Forms.MessageBox.Show("Do you wish to save this drawing?",
"Save Drawing",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question)
== System.Windows.Forms.DialogResult.Yes)
{
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
acDoc.Database.SaveAs(acDoc.Name, true, DwgVersion.Current,
acDoc.Database.SecurityParameters);
}
}
}
//-------------------------------------------
public void OpenDrawing()
{
// strFileName = "C:\\campus.dwg";
//string strFileName = myStr;
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
//Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
if (File.Exists(strFileName))
{
//acDocMgr.Open(strDwgName, false);
DocumentCollectionExtension.Open(acDocMgr, strFileName);
}
else
{
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
" does not exist.");
}
}
//---------------
// public static void AddCircle(Document acDoc)
public static void AddCircle()// My sample code
{
// Get the current document and database
// Document acDoc = Application.DocumentManager.MdiActiveDocument;
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (DocumentLock docLock = acDoc.LockDocument())
{
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
//Here******************************************************************
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//Heres
// Create a circle that is at 2,3 with a radius of 4.25
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(2, 3, 0);
acCirc.Radius = 4.25;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
}
// Save the new object to the database
acTrans.Commit();
}// for lockdocument
}
}