乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 117|回复: 5

[编程交流] C# Drawing file write protecti

[复制链接]

170

主题

347

帖子

174

银币

中流砥柱

Rank: 25

铜币
870
发表于 2022-7-6 21:59:27 | 显示全部楼层 |阅读模式
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 is attached in file
writeProtection Errors.txt
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 22:12:33 | 显示全部楼层
 
I appreciate your including some code (where's the rest of it?), but please instead use [code ] 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
回复

使用道具 举报

170

主题

347

帖子

174

银币

中流砥柱

Rank: 25

铜币
870
发表于 2022-7-6 22:34:01 | 显示全部楼层
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
 
  1. 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[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              }          }    //---------------   }}
回复

使用道具 举报

170

主题

347

帖子

174

银币

中流砥柱

Rank: 25

铜币
870
发表于 2022-7-6 22:38:39 | 显示全部楼层
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.
  1. 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[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              }          }    //---------------   }}
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 2022-7-6 22:48:35 | 显示全部楼层
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
回复

使用道具 举报

170

主题

347

帖子

174

银币

中流砥柱

Rank: 25

铜币
870
发表于 2022-7-6 23:00:31 | 显示全部楼层
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,
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 12:52 , Processed in 0.388651 second(s), 64 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表