using System;
//using Autodesk.AutoCAD.Interop;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
//using Autodesk.AutoCAD.Interop.Common;
using Point = Autodesk.DesignScript.Geometry.Point;
using Autodesk.DesignScript.Runtime;
using Linee = Autodesk.DesignScript.Geometry.Line;
using ArcE = Autodesk.DesignScript.Geometry.Arc;
using PolycurveE = Autodesk.DesignScript.Geometry.PolyCurve;
using PointE = Autodesk.DesignScript.Geometry.Point;
using PolygonE = Autodesk.DesignScript.Geometry.Polygon;
using SurfaceE = Autodesk.DesignScript.Geometry.Surface;
using CircleE = Autodesk.DesignScript.Geometry.Circle;
using NurbsCurveE= Autodesk.DesignScript.Geometry.NurbsCurve;
//using Autodesk.AutoCAD.DatabaseServices;
//using Autodesk.AutoCAD.ApplicationServices;
//using Autodesk.AutoCAD.Interop;
//using System;
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AECC.Interop.UiRoadway;
//using System.Collections.Generic;
//using System;
//using System.IO;
//using System.Text;
//using System.Security.Principal;
//using Autodesk.AutoCAD.ApplicationServices;
//using Autodesk.AutoCAD.DatabaseServices;
//using Autodesk.AutoCAD.Interop.Common;
//using Point = Autodesk.DesignScript.Geometry.Point;
//using Autodesk.AutoCAD.Runtime;
//using Autodesk.AutoCAD.ApplicationServices;
//using Autodesk.AutoCAD.DatabaseServices;
//using Autodesk.AutoCAD.PlottingServices;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Runtime;
//using System.Runtime.InteropServices;
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AutoCAD.Interop.Common;
//using Autodesk.AECC.Interop.UiRoadway;
//using Autodesk.AECC.Interop.Roadway;
//using Autodesk.AECC.Interop.UiLand;
//using Autodesk.AECC.Interop.Land;
//using System.Reflection;
//using Autodesk.DesignScript.Runtime;
//using Autodesk.DesignScript.Geometry;
//using System.Xml;
//using System.Globalization;
//using Application = Autodesk.AutoCAD.ApplicationServices.Application;
//using Autodesk.AutoCAD.EditorInput;
//using Autodesk.AutoCAD.Geometry;
namespace CAD
{
public class CADApplication
{
string m_sAcadProdID = "AutoCAD.Application";
public dynamic m_oAcadApp = null;
public dynamic CadAPP = null;
private Document m_oAeccDoc;
private dynamic InternalElement;
private static dynamic acActiveViewport;
//string m_sAcadProdID = "AutoCAD.Application";
//public AcadApplication m_oAcadApp = null;
//public AcadApplication CadAPP = null;
//private Document m_oAeccDoc;
//private AcadDocument InternalElement;
//private static AcRegenType acActiveViewport;
// Constructor
public CADApplication()
{
this.CadAPP = Get_CAD_Application();
}
internal dynamic Get_CAD_Application()
{
try
{
m_oAcadApp = System.Runtime.InteropServices.Marshal.GetActiveObject(m_sAcadProdID);
}
catch (Autodesk.AutoCAD.Runtime.Exception /*ex*/)
{
System.Type AcadProg = System.Type.GetTypeFromProgID(m_sAcadProdID);
m_oAcadApp = System.Activator.CreateInstance(AcadProg, true);
}
return m_oAcadApp;
}
public static void Draw_Circle(CADApplication CADApplication, double CenterOfCircle_X = 0, double CenterOfCircle_Y = 0, double CenterOfCircle_Z = 0, double RadiusOfCircle = 0)
{
// Definfing the center point for the circle, in this example, we are using origin(0, 0, 0) as the center of circle.
double[] CenterOfCircle = new double[3];
CenterOfCircle[0] = CenterOfCircle_X;
CenterOfCircle[1] = CenterOfCircle_Y;
CenterOfCircle[2] = CenterOfCircle_Z;
// Adding Circle to the modelspace and getting reference to the circle created.
CADApplication.CadAPP.ActiveDocument.ModelSpace.AddCircle(CenterOfCircle, RadiusOfCircle);
}
public static void Draw_Point(CADApplication CADApplication, double X, double Y, double Z)
{
double[] m_point = new double[] { X, Y, Z };
CADApplication.CadAPP.ActiveDocument.ModelSpace.AddPoint(m_point);
}
public static void Draw_Poly_Line(CADApplication CADApplication, IList<double> X, IList<double> Y, IList<double> Z)
{
// Definfing the center point for the circle, in this example, we are using origin(0, 0, 0) as the center of circle.
IList<Point> points = new List<Point>();
for (int i = 0; i < X.Count; i++)
{
points.Add(Point.ByCoordinates(X[i], Y[i], Z[i]));
}
// Adding PolyLine to the modelspace created.
CADApplication.m_oAcadApp.ActiveDocument.ModelSpace.Add3DPoly(points);
}
public static void SendCommand(CADApplication CADApplication, string Command)
{
string Finalcommand;
Finalcommand = Command + "\n";
CADApplication.m_oAcadApp.ActiveDocument.SendCommand(Finalcommand);
}
//public static void SendCommand(CADApplication CADApplication, string Command,string filepath)
//{
// dynamic AcadDoc = CADApplication.m_oAcadApp.Documents.Add();
// AcadDoc.Activate();
// AcadDoc.Close(false);
// string Finalcommand;
// Finalcommand = Command + "\n";
// // CADApplication.m_oAcadApp.Documents.Open(@"C:\Users\mohareb\Desktop\Drawing1.dwg");
// CADApplication.m_oAcadApp.Documents.Open(filepath);
// SendCommand(CADApplication, Command);
//}
public static void SendCommand(CADApplication CADApplication, string Command, string filepath,bool SaveAndClose)
{
dynamic AcadDoc = CADApplication.m_oAcadApp.Documents.Add();
AcadDoc.Activate();
AcadDoc.Close(false);
CADApplication.m_oAcadApp.Documents.Open(filepath);
SendCommand(CADApplication, Command);
if (SaveAndClose)
{
CADApplication.m_oAcadApp.ActiveDocument.Save();
CADApplication.m_oAcadApp.ActiveDocument.Close();
}
}
public override string ToString()
{
return string.Format($"CADApplication (ActiveDocument = {this.m_oAcadApp.ActiveDocument.Name})");
}
[MultiReturn(new[] { "PointToPoint", "LineToLine", "ArcTOArc", "CircleToCircle", "PolyCurveToPolyLine", "PolyCurve_3DToPolyLine", "3DFaceToPlygone", "3DFaceToSurface", "MeshToPoint", "BlockInsertionPoint", "BlockRotationt", "BlockName" , "SplineToNurbsCurve" })] // DesignScript.Runtime
public static Dictionary<string, object> Cad_Geometry(CADApplication CADApplication, string LayerName)
{
// PolycurveE P3d = null;
IList<Linee> LineList = new List<Linee>();
IList<CircleE> CircleList = new List<CircleE>();
IList<ArcE> ArcList = new List<ArcE>();
IList<Point> PointList = new List<Point>();
IList<PolycurveE> PolycurveList = new List<PolycurveE>();
IList<PolycurveE> Polycurve3DList = new List<PolycurveE>();
IList<PolygonE> plygone3DList = new List<PolygonE>();
IList<SurfaceE> SurfaceList = new List<SurfaceE>();
var NurbsCurveList = new List<NurbsCurveE>();
IList<Double> XY = new List<Double>();
IList<Double> XYZ = new List<Double>();
IList<Double> Rotation = new List<Double>();
IList<string> BlockName = new List<string>();
IList<PointE> pointToSpline = new List<PointE>();
IList<PointE> pointToPolyLine = new List<PointE>();
IList<PointE> pointToface = new List<PointE>();
IList<PointE> PointToMesh = new List<PointE>();
IList<PointE> BlockPoint = new List<PointE>();
IList<IList<PointE>> PointToMeshout = new List<IList<PointE>>();
IList<IList<PointE>> BlockPointhout = new List<IList<PointE>>();
// select all element in the layer
short[] oFilter = new short[] { 8 };
object[] oData = new object[] { LayerName };
dynamic objSS = null;
try
{
objSS = CADApplication.m_oAcadApp.ActiveDocument.SelectionSets.item("ssw1");
objSS.Clear();
objSS.Select(Autodesk.AutoCAD.Interop.Common.AcSelect.acSelectionSetAll, null, null, oFilter, oData);
}
catch
{
objSS = CADApplication.m_oAcadApp.ActiveDocument.SelectionSets.Add("ssw1");
objSS.Clear();
objSS.Select(Autodesk.AutoCAD.Interop.Common.AcSelect.acSelectionSetAll, null, null, oFilter, oData);
}
foreach (var obj in objSS)
// foreach (var obj in document.ModelSpace)
{
//////////////////////////////////////////////////////////////////////
if (obj.EntityName == "AcDbSpline")
{
if (obj.Layer == LayerName)
{
foreach (double item in obj.ControlPoints)
{
XYZ.Add(item);
}
for (int i = 0; i < XYZ.Count; i += 3)
{
pointToSpline.Add(Point.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
}
if (obj.Closed)
{
NurbsCurveList.Add( Autodesk.DesignScript.Geometry.NurbsCurve.ByControlPoints(pointToSpline, obj.Degree, true));
// PolycurveList.Add(PolycurveE.ByPoints(pointToPolyLine, true));
}
else
{
var ss = Autodesk.DesignScript.Geometry.NurbsCurve.ByControlPoints(pointToSpline, obj.Degree, false);
NurbsCurveList.Add( Autodesk.DesignScript.Geometry.NurbsCurve.ByControlPoints(pointToSpline, obj.Degree, false));
}
//foreach (double item in obj.FitPoints)
//{
// XY.Add(item);
//}
XYZ.Clear();
}
}
//////////////////////////////////////////////////////////////////////
if (obj.EntityName == "AcDbPoint")
{
foreach (double item in obj.Coordinates)
{
XYZ.Add(item);
}
if (obj.Layer == LayerName)
{
PointList.Add(PointE.ByCoordinates(XYZ[0], XYZ[1], XYZ[2]));
}
XYZ.Clear();
}
////////////////////////////////////////////////////////////////////
if (obj.EntityName == "AcDbLine")
{
Point StartptDyn = Point.ByCoordinates(obj.StartPoint[0], obj.StartPoint[1], obj.StartPoint[2]);
Point EndptDyn = Point.ByCoordinates(obj.EndPoint[0], obj.EndPoint[1], obj.EndPoint[2]);
Linee L = Linee.ByStartPointEndPoint(StartptDyn, EndptDyn);
//if (obj.Layer == LayerName)
//{
LineList.Add(L);
//}
}
/////////////////////////////////////////////////////////////////////
///
if (obj.EntityName == "AcDbArc")
{
Point ArcStartptDyn = Point.ByCoordinates(obj.StartPoint[0], obj.StartPoint[1], obj.StartPoint[2]);
Point ArcEndptDyn = Point.ByCoordinates(obj.EndPoint[0], obj.EndPoint[1], obj.EndPoint[2]);
Point ArcCenterptDyn = Point.ByCoordinates(obj.Center[0], obj.Center[1], obj.Center[2]);
ArcE Arc = ArcE.ByCenterPointStartPointEndPoint(ArcCenterptDyn, ArcStartptDyn, ArcEndptDyn);
//if (obj.Layer == LayerName)
//{
ArcList.Add(Arc);
//}
}
////////////////////////////////////////////////////////////////////////////
if (obj.EntityName == "AcDbCircle")
{
Point Centerpoint = Point.ByCoordinates(obj.Center[0], obj.Center[1], obj.Center[2]);
CircleE C = CircleE.ByCenterPointRadius(Centerpoint, obj.Radius);
//if (obj.Layer == LayerName)
//{
CircleList.Add(C);
//}
}
///////////////////////////////////////////////////
if (obj.EntityName == "AcDbPolyline")
{
double Z = obj.Elevation;
foreach (double item in obj.Coordinates)
{
XY.Add(item);
}
for (int i = 0; i < XY.Count; i += 2)
{
pointToPolyLine.Add(Point.ByCoordinates(XY[i], XY[i + 1], Z));
}
if (obj.Closed)
{
PolycurveList.Add(PolycurveE.ByPoints(pointToPolyLine, true));
}
else
{
PolycurveList.Add(PolycurveE.ByPoints(pointToPolyLine));
}
//if (obj.Layer == LayerName)
//{
//}
XY.Clear();
pointToPolyLine.Clear();
}
////////////////////////////////////////////////
if (obj.EntityName == "AcDb3dPolyline")
{
foreach (double item in obj.Coordinates)
{
XYZ.Add(item);
}
for (int i = 0; i < XYZ.Count; i += 3)
{
pointToPolyLine.Add(Point.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
}
if (true)
{
}
if (obj.Closed)
{
Polycurve3DList.Add(PolycurveE.ByPoints(pointToPolyLine, true));
}
else
{
Polycurve3DList.Add(PolycurveE.ByPoints(pointToPolyLine));
}
//if (obj.Layer == LayerName)
//{
// Polycurve3DList.Add(P3d);
//}
XYZ.Clear();
pointToPolyLine.Clear();
}
////////////////////////////////////////////////////////////////
///
/// //case Acad3DFace Face:
if (obj.EntityName == "AcDbFace")
{
foreach (double item in obj.Coordinates)
{
XYZ.Add(item);
}
for (int i = 0; i < XYZ.Count; i += 3)
{
pointToface.Add(Point.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
}
//if (obj.Layer == LayerName)
//{
plygone3DList.Add(PolygonE.ByPoints(pointToface));
SurfaceList.Add(SurfaceE.ByPatch(PolygonE.ByPoints(pointToface)));
//}
XYZ.Clear();
pointToface.Clear();
}
///////////////////////////////////////////
///
////////////////////////////////////////////////
if (obj.EntityName == "AcDbSubDMesh")
{
foreach (double item in obj.Coordinates)
{
XYZ.Add(item);
}
for (int i = 0; i < XYZ.Count; i += 3)
{
PointToMesh.Add(PointE.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
}
//if (obj.Layer == LayerName)
//{
var object2 = new List<PointE>(PointToMesh);
PointToMeshout.Add(object2);
//}
XYZ.Clear();
PointToMesh.Clear();
}
//////////////////////////////////////////
///
if (obj.EntityName == "AcDbBlockReference")
{
foreach (double item in obj.InsertionPoint)
{
XYZ.Add(item);
}
for (int i = 0; i < XYZ.Count; i += 3)
{
BlockPoint.Add(PointE.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
}
//if (obj.Layer == LayerName)
//{
Double DRotation = obj.Rotation;
string Srting_Block = obj.Name;
Rotation.Add(DRotation/0.0174532925199432958);
BlockName.Add(Srting_Block);
var object2Block = new List<PointE>(BlockPoint);
BlockPointhout.Add(object2Block);
//}
XYZ.Clear();
BlockPoint.Clear();
}
/////////////////
///
// switch (obj)
// {
//////////////////point
//case AcadPoint Po:
// foreach (double item in Po.Coordinates)
// {
// XYZ.Add(item);
// }
// if (Po.Layer == LayerName)
// {
// PointList.Add(PointE.ByCoordinates(XYZ[0], XYZ[1], XYZ[2]));
// }
// XYZ.Clear();
// break;
////////////////Line
// case AcadLine LL:
//case AcadLine LL:
// Point StartptDyn = Point.ByCoordinates(LL.StartPoint[0], LL.StartPoint[1], LL.StartPoint[2]);
// Point EndptDyn = Point.ByCoordinates(LL.EndPoint[0], LL.EndPoint[1], LL.EndPoint[2]);
// Linee L = Linee.ByStartPointEndPoint(StartptDyn, EndptDyn);
// if (LL.Layer == LayerName)
// {
// LineList.Add(L);
// }
// break;
///////// Arc
//case AcadArc Ar:
// Point ArcStartptDyn = Point.ByCoordinates(Ar.StartPoint[0], Ar.StartPoint[1], Ar.StartPoint[2]);
// Point ArcEndptDyn = Point.ByCoordinates(Ar.EndPoint[0], Ar.EndPoint[1], Ar.EndPoint[2]);
// Point ArcCenterptDyn = Point.ByCoordinates(Ar.Center[0], Ar.Center[1], Ar.Center[2]);
// ArcE Arc = ArcE.ByCenterPointStartPointEndPoint(ArcCenterptDyn, ArcStartptDyn, ArcEndptDyn);
// if (Ar.Layer == LayerName)
// {
// ArcList.Add(Arc);
// }
// break;
////////////////Circle
//case AcadCircle Circl:
// Point Centerpoint = Point.ByCoordinates(Circl.Center[0], Circl.Center[1], Circl.Center[2]);
// if (Circl.Layer == LayerName)
// {
// CircleList.Add(CircleE.ByCenterPointRadius(Centerpoint, Circl.Radius));
// }
// break;
//////polyline
//case AcadLWPolyline Poly:
// double Z = Poly.Elevation;
// foreach (double item in Poly.Coordinates)
// {
// XY.Add(item);
// }
// for (int i = 0; i < XY.Count; i += 2)
// {
// pointToPolyLine.Add(Point.ByCoordinates(XY[i], XY[i + 1], Z));
// }
// if (Poly.Layer == LayerName)
// {
// PolycurveList.Add(PolycurveE.ByPoints(pointToPolyLine));
// }
// XY.Clear();
// pointToPolyLine.Clear();
// break;
///////////////////polyline3d
//case Acad3DPolyline Poly3d:
// foreach (double item in Poly3d.Coordinates)
// {
// XYZ.Add(item);
// }
// for (int i = 0; i < XYZ.Count; i += 3)
// {
// pointToPolyLine.Add(Point.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
// }
// if (Poly3d.Layer == LayerName)
// {
// Polycurve3DList.Add(PolycurveE.ByPoints(pointToPolyLine));
// }
// XYZ.Clear();
// pointToPolyLine.Clear();
// break;
///////////////////////3dface
//case Acad3DFace Face:
// foreach (double item in Face.Coordinates)
// {
// XYZ.Add(item);
// }
// for (int i = 0; i < XYZ.Count; i += 3)
// {
// pointToface.Add(Point.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
// }
// if (Face.Layer == LayerName)
// {
// plygone3DList.Add(PolygonE.ByPoints(pointToface));
// SurfaceList.Add(SurfaceE.ByPatch(PolygonE.ByPoints(pointToface)));
// }
// XYZ.Clear();
// pointToface.Clear();
// break;
///////////////////////Mesch
//case AcadSubDMesh MeshD:
// foreach (double item in MeshD.Coordinates)
// {
// XYZ.Add(item);
// }
// for (int i = 0; i < XYZ.Count; i += 3)
// {
// PointToMesh.Add(PointE.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
// }
// if (MeshD.Layer == LayerName)
// {
// var object2 = new List<PointE>(PointToMesh);
// PointToMeshout.Add(object2);
// }
// XYZ.Clear();
// PointToMesh.Clear();
// break;
///////////////////////Block
//case AcadBlockReference Blockk:
// foreach (double item in Blockk.InsertionPoint)
// {
// XYZ.Add(item);
// }
// for (int i = 0; i < XYZ.Count; i += 3)
// {
// BlockPoint.Add(PointE.ByCoordinates(XYZ[i], XYZ[i + 1], XYZ[i + 2]));
// }
// if (Blockk.Layer == LayerName)
// {
// Rotation.Add(Blockk.Rotation);
// BlockName.Add(Blockk.Name);
// var object2Block = new List<PointE>(BlockPoint);
// BlockPointhout.Add(object2Block);
// }
// XYZ.Clear();
// BlockPoint.Clear();
// break;
// }
}
GC.Collect();
GC.WaitForPendingFinalizers();
return new Dictionary<string, object>
{
{ "PointToPoint", PointList },
{ "LineToLine", LineList },
{ "ArcTOArc", ArcList },
{ "CircleToCircle", CircleList },
{ "PolyCurveToPolyLine", PolycurveList },
{ "PolyCurve_3DToPolyLine", Polycurve3DList },
{ "3DFaceToPlygone", plygone3DList },
{ "3DFaceToSurface", SurfaceList },
{ "MeshToPoint", PointToMeshout },
{ "BlockInsertionPoint", BlockPointhout },
{ "BlockRotationt", Rotation },
{ "BlockName", BlockName },
{ "SplineToNurbsCurve", NurbsCurveList },
};
}
}
}
this code work dynamic (to load my pak with other pak accsess to the same com api)
in your case you do not need to use dynamic you can use convert this code (you will found it comment in my code )

it will be better if you will convert it to python (you do not need to use dynamic)
