Search the Community
Showing results for tags 'multiple files'.
-
Hey all, New to C# and trying to slowly get my way around. I can get a variation of my code to work with MDIActiveDocument but what I really want my program to do is allow the user to select a bunch of drawings and then perform some edits to them. I am just trying to get some text inside of an excel file inputted into the .dwg file. Here is my retag.cs file using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; [assembly: CommandClass(typeof(retag.Retag))] namespace retag { internal class Retag { [CommandMethod("DwgRetag")] public void DwgRetag() { // get the excel information ExcelHandler xlHandler = new ExcelHandler(); Dictionary<string, List<(string, string)>> tags = xlHandler.GetTagInfo(); // get the list of .dwgs dwgHandler dwgHandler = new dwgHandler(); List<string> dwgs = dwgHandler.GetListOfDwgs(); dwgHandler.AddTags(tags, dwgs); } } } here is the dwgHandler.cs file using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System.Windows.Forms; using Application = Autodesk.AutoCAD.ApplicationServices.Application; using System.IO; using Exception = System.Exception; namespace retag { internal class dwgHandler { public dwgHandler() { } public List<string> GetListOfDwgs() { List<string> dwgFiles = new List<string>(); // Create an OpenFileDialog to select .dwg files OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "DWG files (*.dwg)|*.dwg", Multiselect = true // Allow multiple file selection }; // Show the dialog and get the result DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { // Add the selected file paths to the list foreach (string filePath in openFileDialog.FileNames) { dwgFiles.Add(filePath); } } return dwgFiles; } public void AddTags(Dictionary<string, List<(string, string)>> tags, List<string> dwgs) { string documentName; // the document name List<(string, string)> dwgTags; // drawing tags foreach (string filePath in dwgs) { dwg acDwg = new dwg(filePath); // get the tags per drawing documentName = Path.GetFileNameWithoutExtension(filePath).ToUpper(); if (tags.TryGetValue(documentName, out dwgTags)) { acDwg.addTags(dwgTags); acDwg.CloseAndSave(); } else { continue; } } } } } and the dwg.cs is: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Colors; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Exception = System.Exception; namespace retag { internal class dwg { private Document doc; private Database acDb; private Editor ed; public dwg(string filePath) { doc = Application.DocumentManager.Open(filePath, false); acDb = doc.Database; ed = doc.Editor; } public void CloseAndSave() { try { // Save the current document doc.Database.SaveAs(doc.Name, true, Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current, doc.Database.SecurityParameters); Console.WriteLine("Document saved successfully."); // Close the document doc.CloseAndDiscard(); Console.WriteLine("Document closed."); } catch (System.Exception ex) { Console.WriteLine($"Error saving or closing the document: {ex.Message}"); // Handle the exception (log, display, etc.) } } public void addTags(List<(string, string)> tags) { // Define the starting coordinates double x = -10.0; double y = 21.0; double verticalGap = 0.2; // Create a new layer for the first tag in each pair string layerName = "old tags"; CreateLayer(layerName); // Iterate through the list of tag pairs foreach (var tagPair in tags) { // Create the first text object (tag1) on the specified layer CreateText(tagPair.Item1, x, y, layerName, addBrackets: true); // Create the second text object (tag2) next to the first one CreateText(tagPair.Item2, x + 1.0, y, "0", addBrackets: false); // Assuming "0" layer for tag2 // Move to the next row y -= verticalGap; } } private ObjectId CreateLayer(string layerName) { using (Transaction tr = this.acDb.TransactionManager.StartTransaction()) { try { LayerTable layerTable = (LayerTable)tr.GetObject(acDb.LayerTableId, OpenMode.ForWrite); if (!layerTable.Has(layerName)) { // Create a new layer if it doesn't exist using (LayerTableRecord newLayer = new LayerTableRecord()) { newLayer.Name = layerName; newLayer.Color = Color.FromColorIndex(ColorMethod.ByAci, 3); // Set color to green ObjectId newLayerId = layerTable.Add(newLayer); tr.AddNewlyCreatedDBObject(newLayer, true); // Commit the transaction tr.Commit(); return newLayerId; } } else { // Layer already exists, return its ObjectId return layerTable[layerName]; } } catch (Exception ex) { // Handle exceptions (log, display, etc.) Console.WriteLine($"Error creating layer: {ex.Message}"); return ObjectId.Null; } } } private void CreateText(string text, double x, double y, string layerName, bool addBrackets) { if (this.doc == null) { // Handle the case where there is no active document return; } using (Transaction tr = this.acDb.TransactionManager.StartTransaction()) { try { BlockTable bt = (BlockTable)tr.GetObject(acDb.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // Add brackets if requested if (addBrackets) text = $"({text})"; DBText textEntity = new DBText { TextString = text, Position = new Point3d(x, y, 0), Layer = layerName }; btr.AppendEntity(textEntity); tr.AddNewlyCreatedDBObject(textEntity, true); // Commit the transaction tr.Commit(); } catch (Exception ex) { // Handle exceptions (log, display, etc.) Console.WriteLine($"Error creating text: {ex.Message}"); } } } } } the main issue i am currently running into is that when it closes and saves, I get an error from autocad saying "error closing/writing file" and in my debug window I am getting "Exception thrown: 'Autodesk.AutoCAD.Runtime.Exception' in Acdbmgd.dll" and I cannot for the life of me figure out why this is happening. My google-fu comes up with stuff that I am confident I am not doing. Any and all help is appreciated.
-
Let’s say you have DWG title block template file, “DAY” with attributes tagged, “B” and “C”. You also have XLSX file, “CAL” with column A rows, (1 thru 7), column B rows, (ONE thru SEV) and column C rows, (SUN thru SAT). How do you use any programing method to create 7 drawings automatically using the template and the worksheet, numbered sequentially so that file “6.dwg” would contain title block displaying “SIX” and “FRI”? Thank You in advance. If I may be any more specific, please ask.
- 2 replies
-
- excel attributes
- automatically
-
(and 1 more)
Tagged with: