Creating autocad drawings using .net (vb and c#) without any user interaction

Creating autocad drawings using .net (vb and c#) without any user interaction

Anonymous
Not applicable
6,480 Views
9 Replies
Message 1 of 10

Creating autocad drawings using .net (vb and c#) without any user interaction

Anonymous
Not applicable

Hello everybody :),
I am new to this forum. I want to create autocad drawings using .net (vb and c#) without any user interaction . Can some one please guide me where to start ?
Where I can find material on this ?

My project has allready started :),

 

Thanks in advance

Nishu

0 Likes
6,481 Views
9 Replies
Replies (9)
Message 2 of 10

SENL1362
Advisor
Advisor

 

//Use Visual Studio to Start a new "Class Library" Project
// Add this code.

 

 

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


using Autodesk.AutoCAD.Runtime;			//Reference: accoremgd, acdbmgd, acmgd
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows.ToolPalette;

// renamings
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcCm = Autodesk.AutoCAD.Colors;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;
using AcLy = Autodesk.AutoCAD.LayerManager;
using AcPl = Autodesk.AutoCAD.PlottingServices;
using AcUi = Autodesk.AutoCAD.Windows;
using PlotType = Autodesk.AutoCAD.DatabaseServices.PlotType;
using AcWin = Autodesk.AutoCAD.Windows;

namespace Firm.AutoCAD.ApplicationName

        [CommandMethod("CUD")]
        public static void CreateUselessDrawings()
        {
            Document doc = null;
            //Database db = null;
            Editor ed = null;


            try
            {

                //To give some feedback, add the Editor (ed)
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc != null)
                    ed = doc.Editor;

                var newDwgPath=Path.GetTempPath();
                if (ed != null)
                    ed.WriteMessage("\n Creating Useless Drawings in: {0}", newDwgPath);

                
                for (int i = 0; i < 4; i++)
                {
                    var buildDefaultDrawing = true;
                    var noDocument = true;

                    using (var newDwgDb = new Database(buildDefaultDrawing, noDocument))
                    {
                        //to make it a bid more usefull add a line
                        using (Transaction tr = newDwgDb.TransactionManager.StartTransaction())
                        {
                            newDwgDb.Measurement = MeasurementValue.Metric;
                            var modelSpace = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(newDwgDb), OpenMode.ForWrite);

                            var pStart = new Point3d(0, 0, 0);
                            var toRad=Math.PI/180;
                            var line_l=100+10*i;
                            var angleInDegrees=i*30;
                            var angleInRad=angleInDegrees*toRad;
                            var xEnd=line_l*Math.Cos(angleInRad);
                            var yEnd=line_l*Math.Sin(angleInRad);
                            var pEnd = new Point3d(xEnd, yEnd, 0);

                            var newLine = new Line(pStart, pEnd);
                            modelSpace.AppendEntity(newLine);
                            tr.AddNewlyCreatedDBObject(newLine, true);

                            tr.Commit();
                        }
                        var newDwgPathname = Path.Combine(newDwgPath, "NewDrawing_" + i + ".dwg");
                        if (File.Exists(newDwgPathname))
                            File.Delete(newDwgPathname);
                        newDwgDb.SaveAs(newDwgPathname, DwgVersion.Current);
                        if (ed != null)
                            ed.WriteMessage("\n\t Created: {0}", Path.GetFileName( newDwgPathname) );
                    }
                }
            }
            catch (System.Exception ex)
            {
                if (ed != null)
                    ed.WriteMessage("\n Error in CreateUselessDrawings: {0}", ex.Message);
            }
        }

     }
}   


 

//Add References to AutoCAD DLL's: accoremgd, acdbmgd, acmgd
//Compile into Application DLL
//Start AutoCAD
//Command: NETLOAD <browse_to_Application DLL>
//Command: CUD

 

 

 This concludes  my project   Smiley Very Happy

 

 

 

 

0 Likes
Message 3 of 10

Anonymous
Not applicable

Hello SENL1362,
Thanks for your reply and sorry for my late reply.
I am trying to understand the code you have given. Let me explain what I am trying to do.
I have a ,net application in vb or c# which accepts data from user, stores it into the sql server, retrieves it whenever needed.
For example user enters size of a rectangle. Radius of a circle which is to be drawn inside that rectangle. Position of the center point of that circle from lower left corner of the rectangle.
And hatch pattern to be applied to the circle as per the material. Now when create drawing button is pressed.
Program will create a new autocad drawing, draw the rectangle and a circle at given position inside that rectangle and apply hatching pattern to that circle.
Save the drawing and open it in autocad. This is to be done through my software, not from autocad. Only after the drawing is created autocad will be opened to show the drawing.

Thanks

0 Likes
Message 4 of 10

SENL1362
Advisor
Advisor

You probably want an out-of-process sample, like this:

http://through-the-interface.typepad.com/through_the_interface/2007/12/launching-autoc.html

 

The sample provided is an in-process sample, so you have to "translate" part of the sample to an out-of-process program.

 

Third methode: a hybride solution, that is Start AutoCAD with a script created by you're program.

acad.exe c:\x\y\thedrawing.dwg /b thescript.scr

TheScript then provide for the required parameters like the size of the rectangle, circle etc.

TheScript also NETLOAD the in-process sample and start the build command.

 

Fourth methode: you're program generate and compile the param specific in-process solution. Then start AutoCAD and create (auto execute) in-process solution.

 

I'll recommend the third methode because you can build and debug more easilly with VisualStudio and AutoCAD.

 

 

 

0 Likes
Message 5 of 10

SENL1362
Advisor
Advisor

Consider this as a sample for the hybride solution

 

       //required input: Width,Height,Radius

        //load script:
        //acad.exe  /b c:/temp/parametric.scr

        //Script sample:
        //NETLOAD c:/temp/parametric.dll
        //PARAMETRICSAMPLE
        //200,150,30
        //SAVEAS
        //2013
        //c:/temp/x.dwg
        //
        [CommandMethod("ParametricSample")]
        public static void ParametricSample()
        {
            Document doc = null;
            Database db = null;
            Editor ed = null;


            try
            {
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                if (doc == null)
                    throw new System.Exception("No MdiActiveDocument");
                db = doc.Database;
                ed = doc.Editor;


                PromptStringOptions paramsPso = new PromptStringOptions("\n Enter Width,Height,Radius: ");
                paramsPso.AllowSpaces = true;
                PromptResult pr = ed.GetString(paramsPso);
                if (pr.Status != PromptStatus.OK) return;
                if (string.IsNullOrWhiteSpace(pr.StringResult)) return;
                var tmpParams = pr.StringResult.Split(',');
                if (tmpParams.Length < 3)
                    throw new System.Exception("Three Params expected");

                double width, height, radius;
                if (!double.TryParse(tmpParams[0], out width) || width  <100)
                    throw new System.Exception("Invalid Width: "+tmpParams[0]);
                if (!double.TryParse(tmpParams[1], out height)|| height  <50)
                    throw new System.Exception("Invalid Height: " + tmpParams[1]);
                if (!double.TryParse(tmpParams[2], out radius)|| radius  <10)
                    throw new System.Exception("Invalid Radius: " + tmpParams[2]);

                //build you're product with these parameters
                ed.WriteMessage("\n Width={0}", width);
                ed.WriteMessage("\n Height={0}", height);
                ed.WriteMessage("\n Radius={0}", radius);

            }
            catch (System.Exception ex)
            {
                if (ed != null)
                    ed.WriteMessage("\n Error in ParametricSample: {0}", ex.Message);
            }
        }

 

 

0 Likes
Message 6 of 10

dgorsman
Consultant
Consultant

Code is the end product, not the starting point.  Sounds like you've got a lot of work to do before you start coding e.g. you need to formalize what your're going to store and how its going to be stored (there's a lot more than just "put in a SQL Server [database]").  You also need to formalize what *isn't* going to be stored.  Getting the data out again is pretty much the reverse of that process.  Then you need to decide how thats going to interact with AutoCAD.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 7 of 10

cadprogramming
Participant
Participant

Sounds like you want to create a plugin.

Autodesk has resources for where to start. http://usa.autodesk.com/adsk/servlet/index?id=18162650&siteID=123112

 

CAD Programming
0 Likes
Message 8 of 10

Anonymous
Not applicable
Hello dgorsman
Yes I understand that. Once I know what and how things can be done I will be able to decide what info needs to be stored in sql.

0 Likes
Message 9 of 10

Anonymous
Not applicable
hi markatcadprogr, link you provided is not working. I have used createobject() with older versions of autocad to create drawings.
0 Likes
Message 10 of 10

BKSpurgeon
Collaborator
Collaborator

Hello,

 

first of all i recommend you go to this thread and read the links me and Keith brown have suggested:

 

 

that will keep you occupied for a good couple of weeks.

 

secondly when you say you do not want user interaction, does that mean you want to automate some work? if so please refer to the above link, and the links within that post.

 

if you truly do not want ANY user interaction, I would suggest you familiarise yourself with Autocad's core console and Script Pro 2.0. Balaji Ram is the grand master in that area:

 

 

 

There you will have to programatically code everything. there will be no GUI (graphical user interface). this means that users cannot draw circles or lines. Well then how do they draw anything? They will have to use the command line. this means they will have to type in words to draw things. but obviously you want everything to be done programmatically so you will have to create a script file. this script file can do the hard work, or it can call a plug in (which you will have pre-programmed). it sounds like you just want to create a plug so just follow the first link.

 

in other words, if what i said makes no sense to you, then perhaps start at the beginner with the very first link. work you way through the material of the next two weeks and then learn some .net and c# and then try to get into Kean's and Balaji's blog. 

 

gluck and best wishes

 

BK

0 Likes