· 5 years ago · Feb 26, 2020, 09:24 AM
1using Autodesk.AutoCAD.ApplicationServices;
2using Autodesk.AutoCAD.BoundaryRepresentation;
3using Autodesk.AutoCAD.DatabaseServices;
4using Autodesk.AutoCAD.EditorInput;
5using Autodesk.AutoCAD.Geometry;
6using System;
7using System.Windows.Media;
8using System.Linq;
9using System.Collections.Generic;
10
11namespace DevAMP
12{
13 public class BoiteOutils
14 {
15 public static void AddExternalBlock(Document acDoc, string blockName, string blockPath)
16 {
17 using (Database OpenDb = new Database(false, true))
18 {
19 OpenDb.ReadDwgFile(blockPath, System.IO.FileShare.ReadWrite, true, "");
20 ObjectIdCollection ids = new ObjectIdCollection();
21 using (Transaction tr = OpenDb.TransactionManager.StartTransaction())
22 {
23 BlockTable bt;
24 bt = (BlockTable)tr.GetObject(OpenDb.BlockTableId, OpenMode.ForWrite);
25 if (bt.Has(blockName))
26 {
27 ids.Add(bt[blockName]);
28 }
29 tr.Commit();
30 }
31 //if found, add the block
32 if (ids.Count != 0)
33 {
34 //get the current drawing database
35 Database destdb = acDoc.Database;
36 IdMapping iMap = new IdMapping();
37 destdb.WblockCloneObjects(ids, destdb.BlockTableId, iMap, DuplicateRecordCloning.Ignore, false);
38 }
39 }
40 }
41
42 public static bool GetCylinderAxis(Solid3d solid, ref Point3d axisPt1, ref Point3d axisPt2)
43 {
44 bool isCylinder = false;
45 axisPt1 = Point3d.Origin;
46 axisPt2 = Point3d.Origin;
47 using (Brep brep = new Brep(solid))
48 {
49 if (brep.Complexes.Count() != 1)
50 return false;
51 if (brep.Faces.Count() != 3)
52 return false;
53 BrepEdgeCollection edges = brep.Edges;
54 if (edges.Count() != 2)
55 return false;
56 CircularArc3d[] circles = brep.Edges
57 .Select(edge =>
58 ((ExternalCurve3d)edge.Curve).NativeCurve
59 as CircularArc3d)
60 .Where(circle => circle != null)
61 .ToArray();
62 if (circles.Length != 2)
63 isCylinder = false;
64 else
65 {
66 isCylinder = (circles[0].Radius == circles[1].Radius && circles[0].Normal.IsParallelTo(circles[1].Normal));
67 axisPt1 = circles[0].Center;
68 axisPt2 = circles[1].Center;
69 }
70 }
71 return isCylinder;
72 }
73
74 /// <summary>
75 /// Method used to create new layer in current Drawing and assign it a color. If the layer already exists, it does nothin
76 /// </summary>
77 /// <param name="acDb">Document's DataBase</param>
78 /// <param name="acEd">Document's Editor</param>
79 /// <param name="acTr">Document's Transaction</param>
80 /// <param name="layerName">Layer's Name</param>
81 /// <param name="color">Set Color from Colors.Enum</param>
82 public static void CreateLayer(Database acDb, Editor acEd, Transaction acTr, string layerName, Color color)
83 {
84 // Get the layer table from the drawing
85 LayerTable lt = (LayerTable)acTr.GetObject(acDb.LayerTableId, OpenMode.ForRead);
86
87 try
88 {
89 // Validate the provided symbol table name
90 SymbolUtilityServices.ValidateSymbolName(layerName, false);
91
92 // Only set the layer name if it isn't in use
93 if (lt.Has(layerName))
94 {
95 acEd.WriteMessage("\nA layer with this name already exists.");
96 return;
97 }
98 }
99 catch
100 {
101 // An exception has been thrown, indicating the name is invalid
102 acEd.WriteMessage("\nInvalid layer name.");
103 }
104
105 // Create our new layer table record...
106 LayerTableRecord ltr = new LayerTableRecord
107 {
108 Name = layerName,
109 Color = Autodesk.AutoCAD.Colors.Color.FromColor(color)
110 };
111
112 // Add the new layer to the layer table
113 lt.UpgradeOpen();
114 ObjectId ltId = lt.Add(ltr);
115 acTr.AddNewlyCreatedDBObject(ltr, true);
116
117 // Set the layer to be current for this drawing
118 acDb.Clayer = ltId;
119 //acEd.WriteMessage("\nCreated layer named \"{0}\" with " + "a color index of {1}.", layerName);
120 }
121
122 public static List<string> AttCollReturn(Tuple<string, string, string> tup)
123 {
124 List<string> attColl = new List<string>();
125 attColl.Add(tup.Item1);
126 attColl.Add(tup.Item2);
127 attColl.Add(tup.Item3);
128 return attColl;
129 }
130
131 public static void InsertBlock2(Transaction acTr, Database acDb, Point3d position, string blockName, double rotation = 0, string layer = "0", List<string> attColl = null)
132 {
133 BlockTable BT = acTr.GetObject(acDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
134 BlockTableRecord BlockMoteur = BT[blockName].GetObject(OpenMode.ForWrite) as BlockTableRecord;
135 BlockTableRecord ms = BT[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
136 using (BlockReference newBlock = new BlockReference(position, BlockMoteur.ObjectId))
137 {
138 ms.AppendEntity(newBlock);
139 newBlock.Rotation = rotation;
140 newBlock.Layer = layer;
141 foreach (ObjectId objID in BlockMoteur)
142 {
143 DBObject dbObj = acTr.GetObject(objID, OpenMode.ForRead) as DBObject;
144
145 if (dbObj is AttributeDefinition)
146 {
147 AttributeDefinition acAtt = dbObj as AttributeDefinition;
148
149 if (!acAtt.Constant)
150 {
151 using (AttributeReference acAttRef = new AttributeReference())
152 {
153 acAttRef.SetAttributeFromBlock(acAtt, newBlock.BlockTransform);
154 acAttRef.TextString = acAtt.TextString;
155 newBlock.AttributeCollection.AppendAttribute(acAttRef);
156 }
157 }
158 }
159 }
160 int i = 0;
161 AttributeCollection attCol = newBlock.AttributeCollection;
162 foreach (ObjectId attId in attCol)
163 {
164 AttributeReference attRef = (AttributeReference)acTr.GetObject(attId, OpenMode.ForWrite);
165 if (i == 3)
166 attRef.TextString = "not Yet";
167 else
168 {
169 attRef.TextString = attColl[i];
170 }
171 i++;
172 }
173 acTr.AddNewlyCreatedDBObject(newBlock, true);
174 }
175 }
176 }
177}