Jump to content

Changing the geometry and moving attributes with C#


docertabum

Recommended Posts

Hello guys,

 

I am absolute noob in this area but I decided to do something with it. I'd like to achieve this: there is a drawing with a geometry - lets say a circle - and attribute definition. What I like to do is to scale everything in the drawing by 2 and additionaly to move attribute to nearest grid point (if an attribute definition is located in [10.1,14.9] it should be moved to [10,15]. To be honest I know how to create new simple objects, but I dont know how to handle existing stuff and have no idea how to query location from attribute definitions.

 

Any help appreciated! Thanks!

 

ps: VS2013/ACAD2015

 

example drawings in 2010/2013 format

http://www40.zippyshare.com/v/37899772/file.html

http://www40.zippyshare.com/v/83044603/file.html

Link to comment
Share on other sites

I did not look at dwg's but I think your using wrong wording a circle does not have attributes it has properties, a block can have attributes. Have you googled make a circle using C# in Autocad I am sure there will be heaps of examples about making/change Autocad objects. Any reason C# pretty high starting language.

 

eg Circle, cen pt, Rad, Colour, Layer, linetype, all these changeable.

Link to comment
Share on other sites

  • 2 weeks later...

I'm sorry I made a naming confusion. I already found out how to handle entities, but now I've encountered another problem. I'm going to process more then 1 file, so I do scale a geometry and then save the file. However doing it by SendStriingToExecute() is not good. It works asynchrously, which means it ends up opening whole bunch of files but not saving and closing as it should. Please check the code and suggest the solution to open the drawing-scale-save-close and then another file. Thank you.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;


namespace SCGRIP
{
   public class Class1
   {
       [CommandMethod("SCGRIP")]

       public void SCGRIP()
       {
           using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\progress.log", true))
           {

               // Dialogbox asks for folder location to be processed
               string folderPath = "";
               FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
               if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
               {
                   folderPath = folderBrowserDialog1.SelectedPath;
               }

               // make a list of drawings to be processed
               string[] fileNames = System.IO.Directory.GetFiles(folderPath, "*.dwg");


               foreach (string fileName in fileNames)
               {

                   Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.Open(fileName, false);
                   Database acCurDb = acDoc.Database;
                   string FilenamePath = acCurDb.Filename;
                   string strDWGName = acDoc.Name;

                   
                   // in the drawing SELECT ALL and scale x20
                   file.WriteLine(">>" + FilenamePath);
                   file.WriteLine("::Select all & scale");

                   acDoc.SendStringToExecute("_ai_selall\n", true, false, false);
                   acDoc.SendStringToExecute("scale\n", true, false, false);
                   acDoc.SendStringToExecute("0,0\n", true, false, false);
                   acDoc.SendStringToExecute("20\n", true, false, false);

                   file.WriteLine("::Save scaled drawing!\n");

                   acDoc.SendStringToExecute("zoom e\n", true, false, false);
                   acDoc.SendStringToExecute("qsave\n", true, false, false);
                   acDoc.SendStringToExecute("close\n", true, false, false);
                   

               }


               
           }
       }
   }
}

Link to comment
Share on other sites

Try this, not tested with forms, just in pure A2014

      //ads_queueexpr // A2014
       [DllImport("accore.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "ads_queueexpr")]
       extern static private int ads_queueexpr(byte[] command);
       //__________________________________________________//
       [CommandMethod("SCGRIP", CommandFlags.Session)]
       public void TestSCGRIP()
       {
           DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("NEXTFIBERWORLD", 1);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMDECHO", 0);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("OSMODE", 0);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("PICKFIRST", 1);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("PICKADD", 2);
           string fileName = @"C:\Test\a_14.dwg";
           Document acDoc = docs.Open(fileName, false);
           Database acCurDb = acDoc.Database;
           string FilenamePath = acCurDb.Filename;
           string strDWGName = acDoc.Name;
           docs.MdiActiveDocument = acDoc;
           System.Text.UnicodeEncoding unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.ZOOM\" \"_Extents\"" + ") "));
           unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(ssget \"_X\"" + ")\n"));
           unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.SCALE\" \"_P\" \"\" \"0,0,0\" \"20.0\") "));
           unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.ZOOM\" \"_Extents\"" + ") "));
           unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.QSAVE\") "));
           unEncode = new System.Text.UnicodeEncoding();
           ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.CLOSE\") "));
       }

Link to comment
Share on other sites

Hi Fixo,

thank you for your response. I tried to implement your solution (i hope I did it right), but it behaves asynchroniously, too. I dont know if forms could be an issue as it is only used to gather a list of files to be processed. Below is the implementation of your idea.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using System.Runtime.InteropServices;
//using Autodesk.AutoCAD.Geometry;

namespace ScaleSaveBezCakania
{
   public class Class1
   {
       //ads_queueexpr // A2014
       [DllImport("accore.dll", CharSet = CharSet.Unicode,CallingConvention = CallingConvention.Cdecl,EntryPoint = "ads_queueexpr")]
       extern static private int ads_queueexpr(byte[] command);
       //__________________________________________________//
       [CommandMethod("SCGRIP", CommandFlags.Session)]
       public void TestSCGRIP()
       {
           DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("NEXTFIBERWORLD", 1);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CMDECHO", 0);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("OSMODE", 0);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("PICKFIRST", 1);
           Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("PICKADD", 2);
           
           // Dialogbox asks for folder location to be processed
           string folderPath = "";
           FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
           if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
           {
               folderPath = folderBrowserDialog1.SelectedPath;
           }

           // make a list of drawings to be processed
           string[] fileNames = System.IO.Directory.GetFiles(folderPath, "*.dwg");


           foreach (string fileName in fileNames)
           {
               Document acDoc = docs.Open(fileName, false);
               Database acCurDb = acDoc.Database;
               string FilenamePath = acCurDb.Filename;
               string strDWGName = acDoc.Name;
               docs.MdiActiveDocument = acDoc;
               System.Text.UnicodeEncoding unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.ZOOM\" \"_Extents\"" + ") "));
               unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(ssget \"_X\"" + ")\n"));
               unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.SCALE\" \"_P\" \"\" \"0,0,0\" \"20.0\") "));
               unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.ZOOM\" \"_Extents\"" + ") "));
               unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.QSAVE\") "));
               unEncode = new System.Text.UnicodeEncoding();
               ads_queueexpr(unEncode.GetBytes("(COMMAND \"_.CLOSE\") "));
           }

       }
   }
}

Link to comment
Share on other sites

Out of curiosity, why P/Invoke Command calls via ads_queueexpr(), in lieu of working directly with .NET API?

 

Cheers

Link to comment
Share on other sites

Out of curiosity, why P/Invoke Command calls via ads_queueexpr(), in lieu of working directly with .NET API?

 

Cheers

I've used it often, though the syntax is too diificult, anyway in 2014 release

it's working good :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...