C# Drawing file write protecti
The following batch loop will work for as far as pulling up a group of drawings and saving themwhen thesample 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 is attached in file
writeProtection Errors.txt
I appreciate your including some code (where's the rest of it?), but please instead use Tags in lieu of attachments.
I'm a fan of DocumentCollection.Add() over DocumentCollectionExtension.Open() personally, but if including bool forReadOnly == False doesn't work for you, consider evaluating the Document.IsReadOnly Property.
Cheers Ok, Here is my code. I renamed my drawing names to Name-New in my batch loop
to allow the loop to work. It creates new drawings with the "-New suffix in order
to avoid the overwrite issue. It is a work-a-round. Not a solution
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.EditorInput;// Added for Autocad//using Autodesk.AutoCAD.Interop.AcadDocument;using Autodesk.AutoCAD.Interop;namespace MyAcadCSharpPlugin1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; } //private string StrSaveName; private string strFileName; private string strDwgName; private string[] thisFormsArray; public string[] myArray { get { return thisFormsArray; } set { thisFormsArray = value; } } private void button1_Click(object sender, EventArgs e) { FormDialog FormDialog = new FormDialog(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(FormDialog); } //on form2: //create a method to show content of array: public void ShowingArray(string[] array) { //1. or to pass to some control: foreach (string item in array) { listBox1.Items.Add(item); } //2. or to show in meesageBox (or similar): StringBuilder sb = new StringBuilder(); foreach (string item in array) { sb.AppendLine(item); } MessageBox.Show(sb.ToString()); } private void Form1_Load(object sender, EventArgs e) { //Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; } private string[] StArray; private void button4_Click(object sender, EventArgs e) { ListDrawings(); } public void ListDrawings() { //Loop through each item in the array listBox1.Items.Clear(); foreach (string myStr in thisFormsArray) { MessageBox.Show(myStr); listBox1.Items.Add(myStr); } } public void DrawingChangeRoutine() { MessageBox.Show("Code Here"); AddCircle();// My Sample Routine // SetLayerCurrent(); }//Posted 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 C //DrawingSaved(); SaveActiveDrawing1(); Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Close drawing doc.CloseAndDiscard(); } } //------------------------------------------- public static void SaveActiveDrawing1() { Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; int StrSaveNameLgh; string strDWGName = acDoc.Name; string StrSaveName; object obj = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGTITLED"); //' Check to see if the drawing has been named if (System.Convert.ToInt16(obj) == 0) { //' If the drawing is using a default name (Drawing1, Drawing2, etc) //' then provide a new name strDWGName = "c:\\MyDrawing1.dwg"; } StrSaveName = strDWGName; StrSaveName = StrSaveName.Remove(StrSaveName.Length - 4, 4); StrSaveName = StrSaveName + "-New" + ".dwg"; MessageBox.Show(StrSaveName); acDoc.Database.SaveAs(StrSaveName, true, DwgVersion.Current, acDoc.Database.SecurityParameters); //acDoc.Database.SaveAs(strDWGName, 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()// 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, 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 } } //--------------- }} I changed & cleaned my code a little.The code will allow me to make new drawings with the
prefix "New-"but I have not found a way to overwrite and edit existing file in C sharp
with this code.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.EditorInput;// Added for Autocad//using Autodesk.AutoCAD.Interop.AcadDocument;using Autodesk.AutoCAD.Interop;namespace MyAcadCSharpPlugin1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; } private string strFileName; private string strDwgName; private string[] thisFormsArray; public string[] myArray { get { return thisFormsArray; } set { thisFormsArray = value; } } private void button1_Click(object sender, EventArgs e) { FormDialog FormDialog = new FormDialog(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(FormDialog); } //on form2: //create a method to show content of array: public void ShowingArray(string[] array) { //1. or to pass to some control: foreach (string item in array) { listBox1.Items.Add(item); } //2. or to show in meesageBox (or similar): StringBuilder sb = new StringBuilder(); foreach (string item in array) { sb.AppendLine(item); } MessageBox.Show(sb.ToString()); } private void Form1_Load(object sender, EventArgs e) { //Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; } private string[] StArray; private void button4_Click(object sender, EventArgs e) { ListDrawings(); } public void ListDrawings() { //Loop through each item in the array listBox1.Items.Clear(); foreach (string myStr in thisFormsArray) { MessageBox.Show(myStr); listBox1.Items.Add(myStr); } } public void DrawingChangeRoutine() { MessageBox.Show("Code Here"); AddCircle();// My Sample Routine // SetLayerCurrent(); }//Posted 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();//Sample code routine to add circle in model space //DrawingSaved(); SaveActiveDrawing1(); Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; //Close drawing doc.CloseAndDiscard(); } } //------------------------------------------- public static void SaveActiveDrawing1() { Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; int StrSaveNameLgh; string strDWGName = acDoc.Name; string StrSaveName; string StrFileName; string StrPathName; String StrPath; object obj = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGTITLED"); //' Check to see if the drawing has been named if (System.Convert.ToInt16(obj) == 0) { //' If the drawing is using a default name (Drawing1, Drawing2, etc) //' then provide a new name strDWGName = "c:\\MyDrawing1.dwg"; } StrPathName = strDWGName; StrPathName = StrPathName.Remove(StrPathName.Length - 4, 4); StrFileName = Path.GetFileName(strDWGName); StrSaveName = StrPathName + "\\" + "New-" + StrFileName; MessageBox.Show(StrSaveName); acDoc.Database.SaveAs(StrSaveName, true, DwgVersion.Current, acDoc.Database.SecurityParameters); //acDoc.Database.SaveAs(strDWGName, true, DwgVersion.Current, acDoc.Database.SecurityParameters); } public void OpenDrawing() { 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()//Sample code routine to add circle in model space { // 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, 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 } } //--------------- }} Sorry no time right now, but real quick - Do you need to open them in the Editor?
Perhaps you should consider open as a side-Database via ReadDwgFile() Method. *not sure*
Cheers Since the above code will make new drawings from my orginals I am in no hurry but
soon or later I would like to find out why my code can't overwrite existing drawings.
Thank you,
页:
[1]