Jump to content

Search the Community

Showing results for tags 'csharp'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • CADTutor
    • News, Announcements & FAQ
    • Feedback
  • AutoCAD
    • AutoCAD Beginners' Area
    • AutoCAD 2D Drafting, Object Properties & Interface
    • AutoCAD Drawing Management & Output
    • AutoCAD 3D Modelling & Rendering
    • AutoCAD Vertical Products
    • AutoCAD LT
    • CAD Management
    • AutoCAD Bugs, Error Messages & Quirks
    • AutoCAD General
    • AutoCAD Blogs
  • AutoCAD Customization
    • The CUI, Hatches, Linetypes, Scripts & Macros
    • AutoLISP, Visual LISP & DCL
    • .NET, ObjectARX & VBA
    • Application Beta Testing
    • Application Archive
  • Other Autodesk Products
    • Autodesk 3ds Max
    • Autodesk Revit
    • Autodesk Inventor
    • Autodesk Software General
  • Other CAD Products
    • BricsCAD
    • SketchUp
    • Rhino
    • SolidWorks
    • MicroStation
    • Design Software
    • Catch All
  • Resources
    • Tutorials & Tips'n'Tricks
    • AutoCAD Museum
    • Blocks, Images, Models & Materials
    • Useful Links
  • Community
    • Introduce Yourself
    • Showcase
    • Work In Progress
    • Jobs & Training
    • Chat
    • Competitions

Categories

  • Programs and Scripts
  • 2D AutoCAD Blocks
  • 3D AutoCAD Blocks
  • Images
    • Backgrounds

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Found 4 results

  1. 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.
  2. Hi there! I wrote a code that, from a selection, generates a polyline and a hatch for every object in the selection. How can I make these objects in the selection when the program is ended? For example: 1. I started my program 2. select the objects I want to evaluate 3. the program creates a polyline and a hatch for every object 4. done Now if I use the command COPY or MOVE (or whatever) I want to select these new objects with the key P (previous) and not the selection I made at point 2 above. Thanks for the help Dennis 2023-02-23-13-07-15.mp4
  3. Ahmed_Hisham

    Autocad 2019 dotnet Wizard

    Good Day , I installed autocad dot net wizard 2019 on my machine and when i opened microsoft visual studio 2017 i cant find the csharp plugin . is there any way to figure out the problem ? Thanks for support. Ahmed
  4. okay, im trying to get a connection to 2006 and im having problems. i used keen(through the interface) example and its throwing a COM exception, BUT it is creating the instance of autocad. I dont think it is connected to it, but it creates it. NOW, when i downloaded the objectARX for 2006 there where no interop.dll's just the 3 "acdbmgd" variations... im just learing how to work with c#. Can some one throw me a little bone here here is my code. public MainForm() { InitializeComponent(); IMessageFilter oldFilter = default(IMessageFilter); CoRegisterMessageFilter(this, oldFilter); AcadApplication acApp = GetAcApp("AutoCAD.Application.16.2"); } public AcadApplication GetAcApp(string progID){ AcadApplication acApp = null; try { acApp = (AcadApplication)Marshal.GetActiveObject(progID); } catch { try { Type acType = Type.GetTypeFromProgID(progID); acApp = (AcadApplication)Activator.CreateInstance(acType,true); } catch { MessageBox.Show("Cannot create object of type \"" + progID + "\""); } } if (acApp != null) { // By the time this is reached AutoCAD is fully // functional and can be interacted with through code acApp.Visible = true; return acApp; } return null; }
×
×
  • Create New...