Author Topic: Templating engine  (Read 1559 times)

0 Members and 1 Guest are viewing this topic.

WILL HATCH

  • Bull Frog
  • Posts: 450
Templating engine
« on: June 19, 2017, 03:47:15 PM »
Hi All,

Cheers and beers! Have been back in the AutoCAD world for the last while again after a couple years on hiatus and I'm having a LOT of fun! For those of you who don't know, or remember, I'm an electrical engineer with a background in industrial facility design, mostly midstream oil and gas. I did not enjoy the interface of the available database driven design tools which are still quite rudimentary, especially considering their high price points.

The progress thus far has rendered a system where devices, instruments, motors, control panels, junction boxes, cable trays, etc. are dropped into the 3d model from a palette and the system connects everything down to the wire termination level, does all the engineering calculations, and outputs all the data required to hand off to a drafter to make the drawing package for construction.

The next step is connecting this to a templating system that will create the final drawing package. I currently have a templating system developed in the early days of my .net experience. It is quite powerful but quite cumbersome. A drawing must be tagged with a bunch of landmarks to show where things go later, then a set of dictionaries to accommodate the drafting standards and block naming conventions of the client.

My problem with this approach is the high amount of repeated work, for example a drawing showing wiring loops for a control panel card with 16 inputs will have 16 'identical' loop definitions offset from each other and explicitly specifying these loops is redundant, and if I want to create a similarly styled drawing I need to respecify each one again.

In the ideal workflow I think would be that the title block would have some data to indicate the drawing area, then we would insert a block to represent a card (or whatever the base of the schematic is) which itself has a drawing area for each loop diagram, then we would insert a block (or maybe just draw stuff) to represent the loop. This way the system could be made more flexible to the typical user through creating different blocks, rather than having to code different methods.

Any thoughts or insights are very appreciated! I'm sure that I'm far from the first person to think about this.

Thanks all!

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Templating engine
« Reply #1 on: June 19, 2017, 03:53:24 PM »
Here's the code for what I got working all those years back if anyone is interested
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Excel = Microsoft.Office.Interop.Excel;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.ComponentModel;
  9. using Autodesk.AutoCAD.Runtime;
  10. using AcApp = Autodesk.AutoCAD.ApplicationServices;
  11. using Autodesk.AutoCAD.DatabaseServices;
  12. using Autodesk.AutoCAD.Geometry;
  13. using Autodesk.AutoCAD.EditorInput;
  14. using Autodesk.AutoCAD.Windows;
  15. using Autodesk.AutoCAD.Colors;
  16. [assembly: CommandClass(typeof(DatabaseToDrawing.DiscreteInput))]
  17.  
  18.  
  19. namespace DatabaseToDrawing
  20. {
  21.     public class DiscreteInput
  22.     {
  23.        
  24.         bool DebugMode = false;
  25.         int ChannelOS = 1, TagNameOS = 2, TagNumberOS = 3, DescripOS = 4,
  26.             AreaOS = 8, TermTypeOS = 9, Term1OS = 10, Term2OS = 12,
  27.             WTYPE12 = 14, WTYPE34 = 15, TermNOS = 16, TermFOS = 19, WireTagsOS = 22;
  28.         int ProgNmOs = 2, GraphNmOs = 3, InsPtOs = 4, WidthOs = 5,
  29.             ScaleOS = 6, ExData = 7, JBOS = 4,
  30.             TextSizeOS = 3;
  31.                
  32.         [CommandMethod("DRAW")]
  33.  
  34.         public void Draw32chDI()
  35.         {
  36.             string LandmarkBlockName = "LANDMARK";
  37.            
  38.             AcApp.Document doc =
  39.             AcApp.Application.DocumentManager.MdiActiveDocument;
  40.             Database db = doc.Database;
  41.             Editor ed = doc.Editor;
  42.             Dictionary<string, BlockSet> BlockDictionary = new Dictionary<string, BlockSet>();
  43.             Dictionary<string, AreaSet> AreaDictionary = new Dictionary<string, AreaSet>();
  44.             Dictionary<string, LayerSet> LayerDictionary = new Dictionary<string, LayerSet>();
  45.             Dictionary<string, double> TextDictionary = new Dictionary<string, double>();
  46.             Dictionary<string, AttributeSet> AttributeDictionary = new Dictionary<string, AttributeSet>();
  47.             PromptKeywordOptions pko = new PromptKeywordOptions("What method?");
  48.             pko.Keywords.Add("Discreet");
  49.             pko.Keywords.Add("Tagging");
  50.             pko.Keywords.Add("Cables");
  51.             PromptResult pkr = ed.GetKeywords(pko);
  52.             if (pkr.Status!=PromptStatus.OK)
  53.                 return;
  54.             string Method = pkr.StringResult;
  55.  
  56.            
  57.             OpenFileDialog ofd = new OpenFileDialog(
  58.                 "Select Dictionary File",
  59.                 "Y:\\Drafting Easier\\",
  60.                 "xlsx; xls",
  61.                 null,
  62.                 OpenFileDialog.OpenFileDialogFlags.SearchPath
  63.                 );
  64.             System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
  65.             if (dr != System.Windows.Forms.DialogResult.OK) return;
  66.             object[,] DictionaryData = ExtractSheetData(ofd.Filename, "Sheet1");
  67.             if (true)
  68.             {
  69.                 object[] Dictionaries = SortToDictionaries(DictionaryData);
  70.                 BlockDictionary = Dictionaries[0] as Dictionary<string, BlockSet>;
  71.                 LayerDictionary = Dictionaries[1] as Dictionary<string, LayerSet>;
  72.                 AreaDictionary = Dictionaries[2] as Dictionary<string, AreaSet>;
  73.                 TextDictionary = Dictionaries[3] as Dictionary<string, double>;
  74.                 AttributeDictionary = Dictionaries[4] as Dictionary<string, AttributeSet>;
  75.             }
  76.             ofd = new OpenFileDialog(
  77.                 "Select Dataset",
  78.                 "Y:\\Drafting Easier\\",
  79.                 "xlsx; xls",
  80.                 null,
  81.                 OpenFileDialog.OpenFileDialogFlags.SearchPath
  82.                 );
  83.             dr = ofd.ShowDialog();
  84.             if (dr != System.Windows.Forms.DialogResult.OK) return;
  85.             object[,] DrawingData = ExtractSheetData(ofd.Filename, "Sheet1");
  86.             List<LandMark> Points = GetDataPoints(doc,db,ed,LandmarkBlockName);
  87.             if (DebugMode)
  88.                 foreach (LandMark lm in Points)
  89.                     ed.WriteMessage("\n {0} {1} {2} {3}", lm.InsertPt, lm.PointNumber, lm.Channel, lm.Section);
  90.                 List<ChannelSet> Channels = SortDataPoints(Points, Method);
  91.            if (DebugMode)
  92.                foreach (ChannelSet cs in Channels)
  93.                     ed.WriteMessage("\n{0} {1} {2} {3} {4} {5} {6} {7} {8}",
  94.                         cs.Fuse.PointNumber, cs.Fuse.Channel, cs.Fuse.Section,
  95.                         cs.Neutral.PointNumber, cs.Neutral.Channel, cs.Neutral.Section,
  96.                         cs.Description.PointNumber, cs.Description.Channel, cs.Description.Section
  97.                     );
  98.             if(DebugMode)
  99.                 ed.WriteMessage("\n 0:{0}, 1:{1}", DrawingData.GetUpperBound(0), DrawingData.GetUpperBound(1));
  100.            
  101.             List<DataSet> DeviceData = SortDataSets(DrawingData, Method);
  102.             ////////////////////////////////////////////////////////////////////
  103.                          
  104.             Transaction tr = doc.TransactionManager.StartTransaction();
  105.             BlockTable bt =
  106.                 (BlockTable)tr.GetObject(
  107.                 db.BlockTableId,
  108.                 OpenMode.ForWrite
  109.                 );
  110.             BlockTableRecord ms =
  111.                 (BlockTableRecord)tr.GetObject(
  112.                 bt[BlockTableRecord.ModelSpace],
  113.                 OpenMode.ForWrite
  114.                 );
  115.             LayerTable lt =
  116.                 tr.GetObject(db.LayerTableId, OpenMode.ForRead)
  117.                 as LayerTable;
  118.             LinetypeTable ltt =
  119.                 tr.GetObject(db.LinetypeTableId, OpenMode.ForRead)
  120.                 as LinetypeTable;
  121.             TextStyleTable tst = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
  122.        
  123.  
  124.             foreach (DataSet DS in DeviceData)
  125.             {
  126.                 foreach (ChannelSet CS in Channels)
  127.                 {
  128.                     if (Method == "Tagging")
  129.                     {
  130.                         if (DS.Channel == CS.Description.Channel)
  131.                         {
  132.                             ObjectId LayerID = lt[LayerDictionary["TEXT"].GraphName];
  133.                             db.Clayer = LayerID;
  134.                             MText CurTag = new MText();
  135.                             CurTag.SetDatabaseDefaults();
  136.                             if (DS.Descriptor1 != null)
  137.                             {
  138.                                 string TagCont = DS.Descriptor1;
  139.                                 if (DS.Descriptor2 != null)
  140.                                 {
  141.                                     TagCont += "\\P" + DS.Descriptor2;
  142.                                     if (DS.Descriptor3 != null)
  143.                                     {
  144.                                         TagCont += "\\P" + DS.Descriptor3;
  145.                                         if (DS.Descriptor4 != null)
  146.                                         {
  147.                                             TagCont += "\\P" + DS.Descriptor4;
  148.                                         }
  149.                                     }
  150.                                 }
  151.  
  152.                                 CurTag.Contents = TagCont;
  153.                             }
  154.                             else
  155.                                 continue;
  156.                             try
  157.                             {
  158.                                 ObjectId styleID = tst[DS.TermType]; //DS.TermType holds text style
  159.                                 CurTag.TextStyleId = styleID;
  160.                             }
  161.                             catch (System.Exception)
  162.                             {
  163.                                 ed.WriteMessage("\n{0} Text Style does not exist!", DS.TermType);
  164.                             }
  165.                             try
  166.                             {
  167.  
  168.                                 if (DS.HomeRun == "UP") //DS.HomeRun holds direction
  169.                                     CurTag.Rotation = Math.PI / 2;
  170.                                 else if (DS.HomeRun == "DOWN")
  171.                                     CurTag.Rotation = Math.PI * 3 / 2;
  172.                                 else if (DS.HomeRun == "LEFT")
  173.                                     CurTag.Rotation = Math.PI;
  174.                             }
  175.                             catch (System.Exception)
  176.                             {
  177.                                 ed.WriteMessage("\n{0} is an invalid text direction", DS.HomeRun);
  178.                             }
  179.                             switch (DS.TagName)
  180.                             {
  181.                                 case "MR":
  182.                                     CurTag.Attachment = AttachmentPoint.MiddleRight;
  183.                                     break;
  184.                                 case "ML":
  185.                                     CurTag.Attachment = AttachmentPoint.MiddleLeft;
  186.                                     break;
  187.                                 case "MC":
  188.                                     CurTag.Attachment = AttachmentPoint.MiddleCenter;
  189.                                     break;
  190.                                 case "BR":
  191.                                     CurTag.Attachment = AttachmentPoint.BottomRight;
  192.                                     break;
  193.                                 case "BL":
  194.                                     CurTag.Attachment = AttachmentPoint.BottomLeft;
  195.                                     break;
  196.                                 case "BC":
  197.                                     CurTag.Attachment = AttachmentPoint.BottomCenter;
  198.                                     break;
  199.                                 case "TR":
  200.                                     CurTag.Attachment = AttachmentPoint.TopRight;
  201.                                     break;
  202.                                 case "TL":
  203.                                     CurTag.Attachment = AttachmentPoint.TopLeft;
  204.                                     break;
  205.                                 case "TC":
  206.                                     CurTag.Attachment = AttachmentPoint.TopCenter;
  207.                                     break;
  208.                                 default:
  209.                                     ed.WriteMessage("\nERROR! {0} Not Found!", DS.TagName);
  210.                                     break;
  211.                             }
  212.                             CurTag.Location = CS.Description.InsertPt;
  213.                             double TextHeight;
  214.                             try
  215.                             {
  216.                                 TextHeight = Convert.ToDouble(DS.TagNumber);
  217.                             }
  218.                             catch (FormatException)
  219.                             {
  220.                                 ed.WriteMessage("\nInvalid Text Height for {0}",DS.Channel);
  221.                                 TextHeight = 2.5;
  222.                             }
  223.                             CurTag.TextHeight = TextHeight;
  224.                            
  225.                             ObjectId mtID = ms.AppendEntity(CurTag);
  226.                             tr.AddNewlyCreatedDBObject(CurTag, true);
  227.                         }
  228.                     }

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Templating engine
« Reply #2 on: June 19, 2017, 03:54:17 PM »
code continued
Code - C#: [Select]
  1. else if (Method == "Discreet")
  2.                     {
  3.                         if (DS.Channel == CS.Fuse.Channel)
  4.                         {
  5.  
  6.                             if (DebugMode)
  7.                                 ed.WriteMessage("\n{0},{1},{2},{3},{4},{5}",
  8.                                     DS.TagName, DS.Channel, DS.Area, DS.HomeRun, DS.Term1Tag1, CS.Fuse.Channel);
  9.                             ///////////////////////
  10.                             double XVector = CS.Fuse.InsertPt.X - CS.Neutral.InsertPt.X;
  11.                             double UnitXVector = XVector / Math.Abs(XVector);
  12.                             double StdSymbolWidth = BlockDictionary[DS.TagName].Width * BlockDictionary[DS.TagName].Scale;
  13.                             double quarterX = (CS.Fuse.InsertPt.X - CS.Neutral.InsertPt.X) / 4;
  14.                             Point3d CenterPt = new Point3d(
  15.                                 (CS.Neutral.InsertPt.X + CS.Fuse.InsertPt.X) / 2,
  16.                                 (CS.Neutral.InsertPt.Y + CS.Fuse.InsertPt.Y) / 2,
  17.                                 (CS.Neutral.InsertPt.Z + CS.Fuse.InsertPt.Z) / 2
  18.                                 );
  19.  
  20.                             ObjectId LayerID = lt[LayerDictionary["TEXT"].GraphName];
  21.                             db.Clayer = LayerID;
  22.                             MText Descriptor = new MText();
  23.                             Descriptor.SetDatabaseDefaults();
  24.                             Descriptor.Contents = DS.Descriptor1 + "\\P" + DS.Descriptor2 +
  25.                                 "\\P" + DS.Descriptor3 + "\\P" + DS.Descriptor4;
  26.                             Descriptor.Location = CS.Description.InsertPt;
  27.                             Descriptor.Attachment = AttachmentPoint.MiddleLeft;
  28.                             Descriptor.TextHeight = TextDictionary["LBL"];
  29.                             ObjectId mtID = ms.AppendEntity(Descriptor);
  30.                             tr.AddNewlyCreatedDBObject(Descriptor, true);
  31.                             if (DebugMode) ed.WriteMessage("\n1");
  32.                             AttachmentPoint[] attPts = GetMtextAttachment(UnitXVector);
  33.                             Point3d[] insertPts = GetMtextInserts(
  34.                                 UnitXVector, StdSymbolWidth, TextDictionary["WTAG"], CS.Neutral.InsertPt, CS.Fuse.InsertPt);
  35.                             for (int i = 0; i < 8; i++)
  36.                             {
  37.                                 if (DS.WireTags[i] != null)
  38.                                 {
  39.                                     MText WireTag = new MText();
  40.                                     WireTag.SetDatabaseDefaults();
  41.                                     WireTag.Attachment = attPts[i];
  42.                                     WireTag.Contents = DS.WireTags[i];
  43.                                     WireTag.Location = insertPts[i];
  44.                                     WireTag.TextHeight = TextDictionary["WTAG"];
  45.                                     mtID = ms.AppendEntity(WireTag);
  46.                                     tr.AddNewlyCreatedDBObject(WireTag, true);
  47.                                 }
  48.                             }
  49.  
  50.                             ///////////////////////
  51.  
  52.                             if (bt.Has(BlockDictionary[DS.TagName].GraphName))
  53.                             {
  54.                                 LayerID = lt[LayerDictionary["SYMBOL"].GraphName];
  55.                                 db.Clayer = LayerID;
  56.                                 BlockTableRecord SymbolBlockRecord =
  57.                                     (BlockTableRecord)tr.GetObject(
  58.                                     bt[BlockDictionary[DS.TagName].GraphName],
  59.                                     OpenMode.ForRead
  60.                                     );
  61.                                 BlockReference SymbolBlock = new BlockReference(new Point3d(
  62.                                     CenterPt.X + BlockDictionary[DS.TagName].InsertPt,
  63.                                     CenterPt.Y, CenterPt.Z), SymbolBlockRecord.ObjectId
  64.                                     );
  65.                                 SymbolBlock.ScaleFactors = new Scale3d(BlockDictionary[DS.TagName].Scale);
  66.                                 ms.AppendEntity(SymbolBlock);
  67.                                 tr.AddNewlyCreatedDBObject(SymbolBlock, true);
  68.                                 ed.WriteMessage("\n {0} {1}", DS.TagNumber, SymbolBlockRecord.Name);
  69.                                 if (SymbolBlockRecord.HasAttributeDefinitions)
  70.                                 {
  71.  
  72.                                     ed.WriteMessage(" Has {0} Attibutes:", SymbolBlock.AttributeCollection.Count);
  73.                                     foreach (ObjectId id in SymbolBlockRecord)
  74.                                     {
  75.                                         DBObject obj = tr.GetObject(id, OpenMode.ForRead);
  76.                                         AttributeDefinition ad = obj as AttributeDefinition;
  77.                                         if (ad != null && !ad.Constant)
  78.                                         {
  79.                                             AttributeReference ar = new AttributeReference();
  80.                                             ar.SetAttributeFromBlock(ad, SymbolBlock.BlockTransform);
  81.                                             ar.Position = ad.Position.TransformBy(SymbolBlock.BlockTransform);
  82.                                             if (ad.Justify != AttachmentPoint.BaseLeft)
  83.                                                 ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(SymbolBlock.BlockTransform);
  84.                                             ar.SetFromStyle();
  85.                                             ed.WriteMessage("|{0}|", ar.Tag);
  86.                                             if (ar.IsMTextAttribute)
  87.                                             {
  88.                                                 ar.UpdateMTextAttribute();
  89.                                                 ed.WriteMessage("\nMtext Attributes! >.<");
  90.                                             }
  91.                                             if (!AttributeDictionary.ContainsKey(SymbolBlock.Name))
  92.                                                 ed.WriteMessage(" \n {0} \n", SymbolBlock.Name);
  93.                                             else if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute1)
  94.                                             {
  95.                                                 ar.TextString = DS.TagNumber;
  96.                                                 ObjectId arID = SymbolBlock.AttributeCollection.AppendAttribute(ar);
  97.                                                 tr.AddNewlyCreatedDBObject(ar, true);
  98.                                                 //if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute1)
  99.                                                 //    ar.TextString = DS.TagName;
  100.                                                 ////if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute2)
  101.                                                 //    ar.TextString = AttributeDictionary[SymbolBlock.Name].Attribute2;
  102.                                                 //if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute3)
  103.                                                 //    ar.TextString = AttributeDictionary[SymbolBlock.Name].Attribute3;
  104.                                                 //if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute4)
  105.                                                 //    ar.TextString = AttributeDictionary[SymbolBlock.Name].Attribute4;
  106.                                                 //if (ar.Tag == AttributeDictionary[SymbolBlock.Name].Attribute5)
  107.                                                 //    ar.TextString = AttributeDictionary[SymbolBlock.Name].Attribute5;
  108.                                             }
  109.                                             ed.WriteMessage("2 Has {0} Attibutes:", SymbolBlock.AttributeCollection.Count);
  110.  
  111.                                         }
  112.                                     }
  113.                                 }
  114.                                 if (DS.TermType != null)
  115.                                 {
  116.                                     double StdTermWidth =
  117.                                         BlockDictionary[DS.TermType].Width * BlockDictionary[DS.TermType].Scale;
  118.                                     if (bt.Has(BlockDictionary[DS.TermType].GraphName))
  119.                                     {
  120.                                         SymbolBlockRecord = (BlockTableRecord)tr.GetObject(
  121.                                             bt[BlockDictionary[DS.TermType].GraphName],
  122.                                             OpenMode.ForRead);
  123.                                         SymbolBlock = new BlockReference(new Point3d(
  124.                                             CS.Neutral.InsertPt.X + quarterX + BlockDictionary[DS.TermType].InsertPt,
  125.                                             CenterPt.Y, CenterPt.Z), SymbolBlockRecord.ObjectId);
  126.                                         SymbolBlock.ScaleFactors = new Scale3d(BlockDictionary[DS.TermType].Scale);
  127.                                         ms.AppendEntity(SymbolBlock);
  128.                                         tr.AddNewlyCreatedDBObject(SymbolBlock, true);
  129.                                         SymbolBlock = new BlockReference(new Point3d(
  130.                                             CS.Neutral.InsertPt.X + 3 * quarterX + BlockDictionary[DS.TermType].InsertPt,
  131.                                             CenterPt.Y, CenterPt.Z), SymbolBlockRecord.ObjectId);
  132.                                         SymbolBlock.ScaleFactors = new Scale3d(BlockDictionary[DS.TermType].Scale);
  133.                                         ms.AppendEntity(SymbolBlock);
  134.                                         tr.AddNewlyCreatedDBObject(SymbolBlock, true);
  135.                                     }
  136.                                     if (bt.Has(BlockDictionary[DS.TermType].GraphName))
  137.                                     {
  138.                                         SymbolBlockRecord =
  139.                                             (BlockTableRecord)tr.GetObject(
  140.                                             bt[BlockDictionary[DS.TermType].GraphName],
  141.                                             OpenMode.ForRead);
  142.                                         SymbolBlock = new BlockReference(new Point3d(
  143.                                             CS.Neutral.InsertPt.X + quarterX + BlockDictionary[DS.TermType].InsertPt,
  144.                                             CenterPt.Y, CenterPt.Z), SymbolBlockRecord.ObjectId);
  145.                                         SymbolBlock.ScaleFactors = new Scale3d(BlockDictionary[DS.TermType].Scale);
  146.                                         ms.AppendEntity(SymbolBlock);
  147.                                         tr.AddNewlyCreatedDBObject(SymbolBlock, true);
  148.                                         SymbolBlock = new BlockReference(new Point3d(
  149.                                             CS.Neutral.InsertPt.X + 3 * quarterX + BlockDictionary[DS.TermType].InsertPt,
  150.                                             CenterPt.Y, CenterPt.Z), SymbolBlockRecord.ObjectId);
  151.                                         SymbolBlock.ScaleFactors = new Scale3d(BlockDictionary[DS.TermType].Scale);
  152.                                         ms.AppendEntity(SymbolBlock);
  153.                                         tr.AddNewlyCreatedDBObject(SymbolBlock, true);
  154.                                     }
  155.                                 }
  156.                             }
  157.                             else ed.WriteMessage("\nError with {0}", BlockDictionary[DS.TagName].GraphName);
  158.                             if (DS.TermType == null)
  159.                             {
  160.                                 ObjectId ltID = new ObjectId();
  161.                                 ltID = lt[LayerDictionary[DS.WType12].GraphName];
  162.                                 db.Clayer = ltID;
  163.                                 Line Line = new Line(CS.Neutral.InsertPt, new Point3d(
  164.                                     CenterPt.X - UnitXVector * StdSymbolWidth / 2,
  165.                                     CenterPt.Y, CenterPt.Z
  166.                                 ));
  167.                                 ms.AppendEntity(Line);
  168.                                 tr.AddNewlyCreatedDBObject(Line, true);
  169.                                 Line = new Line(CS.Fuse.InsertPt, new Point3d(
  170.                                     CenterPt.X + UnitXVector * StdSymbolWidth / 2,
  171.                                     CenterPt.Y, CenterPt.Z
  172.                                 ));
  173.                                 ms.AppendEntity(Line);
  174.                                 tr.AddNewlyCreatedDBObject(Line, true);
  175.                                 if (DebugMode)
  176.                                     ed.WriteMessage("\n{0} {1} {2} {3}",
  177.                                     DS.TagName, BlockDictionary[DS.TagName].Width, BlockDictionary[DS.TagName].Scale,
  178.                                     StdSymbolWidth);
  179.                             }
  180.                             else
  181.                             {
  182.                                 double StdTermWidth =
  183.                                     BlockDictionary[DS.TermType].Width * BlockDictionary[DS.TermType].Scale;
  184.                                 ObjectId ltID = new ObjectId();
  185.                                 ltID = lt[LayerDictionary[DS.WType12].GraphName];
  186.                                 db.Clayer = ltID;
  187.                                 Line Line0 = new Line(CS.Neutral.InsertPt, new Point3d(
  188.                                     CS.Neutral.InsertPt.X + quarterX - UnitXVector * StdTermWidth / 2,
  189.                                     (CS.Fuse.InsertPt.Y + CS.Neutral.InsertPt.Y) / 2,
  190.                                     (CS.Fuse.InsertPt.Z + CS.Neutral.InsertPt.Z) / 2
  191.                                     ));
  192.                                 ms.AppendEntity(Line0);
  193.                                 tr.AddNewlyCreatedDBObject(Line0, true);
  194.                                 Line0 = new Line(CS.Fuse.InsertPt, new Point3d(
  195.                                     CS.Neutral.InsertPt.X + 3 * quarterX + UnitXVector * StdTermWidth / 2,
  196.                                     (CS.Fuse.InsertPt.Y + CS.Neutral.InsertPt.Y) / 2,
  197.                                     (CS.Fuse.InsertPt.Z + CS.Neutral.InsertPt.Z) / 2
  198.                                     ));
  199.                                 ms.AppendEntity(Line0);
  200.                                 tr.AddNewlyCreatedDBObject(Line0, true);
  201.                                 ltID = lt[LayerDictionary[DS.WType34].GraphName];
  202.                                 db.Clayer = ltID;
  203.                                 Line0 = new Line(new Point3d(
  204.                                     CenterPt.X - StdSymbolWidth * UnitXVector / 2,
  205.                                     CenterPt.Y, CenterPt.Z), new Point3d(
  206.                                     CS.Neutral.InsertPt.X + quarterX + UnitXVector * StdTermWidth / 2,
  207.                                     CenterPt.Y, CenterPt.Z));
  208.                                 ms.AppendEntity(Line0);
  209.                                 tr.AddNewlyCreatedDBObject(Line0, true);
  210.                                 Line0 = new Line(new Point3d(
  211.                                     CenterPt.X + StdSymbolWidth * UnitXVector / 2,
  212.                                     CenterPt.Y, CenterPt.Z), new Point3d(
  213.                                     CS.Neutral.InsertPt.X + 3 * quarterX - UnitXVector * StdTermWidth / 2,
  214.                                     CenterPt.Y, CenterPt.Z));
  215.                                 ms.AppendEntity(Line0);
  216.                                 tr.AddNewlyCreatedDBObject(Line0, true);
  217.                                 if (DebugMode)
  218.                                     ed.WriteMessage("\n{0} {1} {2} {3} {4} {5} {6}",
  219.                                     DS.TagName, BlockDictionary[DS.TermType].Scale, BlockDictionary[DS.TermType].Width,
  220.                                     BlockDictionary[DS.TagName].Width, BlockDictionary[DS.TagName].Scale, StdSymbolWidth, StdTermWidth);
  221.  
  222.                             }
  223.                         }
  224.                     }
  225.                 }
  226.             }
  227.             tr.Commit();
  228.             tr.Dispose();
  229.         }

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Templating engine
« Reply #3 on: June 19, 2017, 03:55:34 PM »
still continued...
Code - C#: [Select]
  1. public class LandMark
  2.         {
  3.             public Point3d InsertPt { get; set; }
  4.             public string PointNumber { get; set; }
  5.             public string Channel { get; set; }
  6.             public string Section { get; set; }
  7.             public bool Checked { get; set; }
  8.         }
  9.         class ChannelSet
  10.         {
  11.             public LandMark Fuse { get; set; }
  12.             public LandMark Neutral { get; set; }
  13.             public LandMark Description { get; set; }
  14.         }
  15.         class DataSet
  16.         {
  17.             public string Channel { get; set; }
  18.             public string TagName { get; set; }
  19.             public string TagNumber { get; set; }
  20.             public string Descriptor1 { get; set; }
  21.             public string Descriptor2 { get; set; }
  22.             public string Descriptor3 { get; set; }
  23.             public string Descriptor4 { get; set; }
  24.             public string Area { get; set; }
  25.             public string HomeRun { get; set; }
  26.             public string TermType { get; set; }
  27.             public string Term1Tag1 { get; set; }
  28.             public string Term1Tag2 { get; set; }
  29.             public string Term2Tag1 { get; set; }
  30.             public string Term2Tag2 { get; set; }
  31.             public string WType12 { get; set; }
  32.             public string WType34 { get; set; }
  33.             public string TermNType { get; set; }
  34.             public string TermNTag1 { get; set; }
  35.             public string TermNTag2 { get; set; }
  36.             public string TermFType { get; set; }
  37.             public string TermFTag1 { get; set; }
  38.             public string TermFTag2 { get; set; }
  39.             public string[] WireTags { get; set; }
  40.         }
  41.         class BlockSet
  42.         {
  43.             public string GraphName { get; set; }
  44.             public double InsertPt { get; set; }
  45.             public double Width { get; set; }
  46.             public double Scale { get; set; }
  47.             public string ExData { get; set; }
  48.         }
  49.         class LayerSet
  50.         {
  51.             public string GraphName { get; set; }
  52.         }
  53.         class AreaSet
  54.         {
  55.             public string JunctionBoxes { get; set; }
  56.         }
  57.         class AttributeSet
  58.         {
  59.             public string Attribute1 { get; set; }
  60.             public string Attribute2 { get; set; }
  61.             public string Attribute3 { get; set; }
  62.             public string Attribute4 { get; set; }
  63.             public string Attribute5 { get; set; }
  64.         }
  65.         public object[,] ExtractSheetData(
  66.             string FilePath,
  67.             string SheetNumber
  68.             )
  69.         {
  70.             //open excel
  71.             Excel.Application xlApp = new Excel.Application();
  72.             Excel.Workbook xlWorkBook = default(Excel.Workbook);
  73.             xlApp = new Excel.Application();
  74.             xlApp.Visible = true;
  75.             //open workbook
  76.             xlWorkBook = xlApp.Workbooks.Open(FilePath);
  77.             //open worksheets, load into arrays
  78.             Excel.Worksheet xlWorkSheet1 = xlWorkBook.Sheets[SheetNumber] as Excel.Worksheet;
  79.             Excel.Range r = xlWorkSheet1.UsedRange;
  80.             object[,] Sheet1;
  81.             Sheet1 = (object[,])r.Value2;
  82.             xlWorkBook.Close();
  83.             xlApp.Quit();
  84.             return Sheet1;
  85.         }
  86.         /// <summary>
  87.         /// Extracts landmark blocks from a template drawing and return a list of landmark objects
  88.         /// </summary>
  89.         /// <param name="doc">Applicable AutoCAD Document</param>
  90.         /// <param name="db">Applicable AutoCAD Database</param>
  91.         /// <param name="ed">AutoCAD Editor</param>
  92.         /// <param name="BlockName">LandMark Block Name</param>
  93.         /// <returns>List of LandMark Objects</returns>
  94.         public List<LandMark> GetDataPoints(
  95.             AcApp.Document doc, Database db, Editor ed, string BlockName
  96.             )
  97.         {
  98.             Transaction tr = doc.TransactionManager.StartTransaction();
  99.             BlockTable bt =
  100.                 (BlockTable)tr.GetObject(
  101.                 db.BlockTableId,
  102.                 OpenMode.ForRead
  103.                 );
  104.             BlockTableRecord ms =
  105.                 (BlockTableRecord)tr.GetObject(
  106.                 bt[BlockTableRecord.ModelSpace],
  107.                 OpenMode.ForRead
  108.                 );
  109.             int BlockCount = 0;
  110.             string Att1 = "", Att2 = "", Att3 = "";
  111.             List<LandMark> Points = new List<LandMark>();
  112.             foreach (ObjectId id in ms)
  113.             {
  114.                 Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
  115.                 if (ent != null)
  116.                 {
  117.                     BlockReference br = ent as BlockReference;
  118.                     if (br != null)
  119.                     {
  120.                         BlockTableRecord bd =
  121.                             (BlockTableRecord)tr.GetObject(
  122.                             br.BlockTableRecord,
  123.                             OpenMode.ForRead
  124.                             );
  125.                         if (bd.Name.ToUpper() == BlockName)
  126.                         {
  127.                             if (DebugMode == true)
  128.                             {
  129.                                 BlockCount += 1;
  130.                                 ed.WriteMessage("\n block {1} at {0} attributes:", br.Position, BlockCount, br.Layer);
  131.                             }
  132.                             int AttCount = 0;
  133.                             foreach (ObjectId oID in br.AttributeCollection)
  134.                             {
  135.                                 AttributeReference ar =
  136.                                     (AttributeReference)tr.GetObject(oID, OpenMode.ForRead);
  137.                                 AttCount += 1;
  138.                                 switch (AttCount)
  139.                                 {
  140.                                     case 1:
  141.                                         Att1 = ar.TextString;
  142.                                         break;
  143.                                     case 2:
  144.                                         Att2 = ar.TextString;
  145.                                         break;
  146.                                     case 3:
  147.                                         Att3 = ar.TextString;
  148.                                         break;
  149.                                     default:
  150.                                         ed.WriteMessage("\nGetDataPoints() Switch Overflow!");
  151.                                         break;
  152.                                 }
  153.                                 //ed.WriteMessage(" {0}:{1},", ar.Tag, ar.TextString);
  154.                             }
  155.                             Points.Add(new LandMark()
  156.                             {
  157.                                 InsertPt = br.Position,
  158.                                 PointNumber = Att1,
  159.                                 Channel = Att2,
  160.                                 Section = Att3,
  161.                                 Checked = false
  162.                             });
  163.                             //br.UpgradeOpen();
  164.                             //br.Erase();
  165.                         }
  166.                     }
  167.                 }
  168.             }
  169.             tr.Commit();
  170.             tr.Dispose();
  171.             return Points;
  172.         }
  173.         /// <summary>
  174.         /// Sorts out channel sets from a given list of LandMark objects
  175.         /// </summary>
  176.         /// <param name="Points">List of LandMark objects</param>
  177.         /// <returns>List of ChannelSet objects</returns>
  178.         private List<ChannelSet> SortDataPoints(List<LandMark> Points, string Method)
  179.         {
  180.             List<ChannelSet> Channels = new List<ChannelSet>();
  181.             List<LandMark> NewPoints = new List<LandMark>();
  182.             NewPoints = Points;
  183.             if (Method == "Discreet")
  184.             {
  185.                 foreach (LandMark lm0 in Points)
  186.                 {
  187.                     LandMark Fuse = new LandMark();
  188.                     LandMark Neutral = new LandMark();
  189.                     LandMark Description = new LandMark();
  190.                     if (lm0.Checked == false)
  191.                     {
  192.                         List<LandMark> CurrentSet = new List<LandMark>();
  193.                         CurrentSet.Add(lm0);
  194.                         lm0.Checked = true;
  195.                         foreach (LandMark lm1 in Points)
  196.                         {
  197.                             if (lm0.Channel == lm1.Channel)
  198.                             {
  199.                                 CurrentSet.Add(lm1);
  200.                                 lm1.Checked = true;
  201.                             }
  202.                         }
  203.                         foreach (LandMark lm in CurrentSet)
  204.                         {
  205.                             switch (lm.Section)
  206.                             {
  207.                                 case "f":
  208.                                     Fuse = lm;
  209.                                     break;
  210.                                 case "n":
  211.                                     Neutral = lm;
  212.                                     break;
  213.                                 case "t":
  214.                                     Description = lm;
  215.                                     break;
  216.                                 default:
  217.                                     break;
  218.                             }
  219.                         }
  220.                         if (Fuse.Section == null | Neutral.Section == null | Description.Section == null)
  221.                         {
  222.                             AcApp.Document doc =
  223.                                AcApp.Application.DocumentManager.MdiActiveDocument;
  224.                             Editor ed = doc.Editor;
  225.                             ed.WriteMessage("\nError adding {0}: {1},{2},{3}",
  226.                                 Fuse.Channel, Fuse.Section, Neutral.Section, Description.Section);
  227.  
  228.                         }
  229.                         else
  230.                         {
  231.                             ChannelSet CurrCS = new ChannelSet()
  232.                             {
  233.                                 Fuse = Fuse,
  234.                                 Neutral = Neutral,
  235.                                 Description = Description
  236.                             };
  237.                             Channels.Add(CurrCS);
  238.                         }
  239.  
  240.                     }
  241.                 }
  242.             }
  243.             if (Method == "Tagging")
  244.             {
  245.                 foreach (LandMark lm in Points)
  246.                 {
  247.                     ChannelSet CurrCS = new ChannelSet()
  248.                     {
  249.                         Fuse = null,
  250.                         Neutral = null,
  251.                         Description = lm
  252.                     };
  253.                     Channels.Add(CurrCS);
  254.                 }
  255.             }
  256.             return Channels;
  257.         }
  258.        

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Templating engine
« Reply #4 on: June 19, 2017, 03:56:04 PM »
and finally all the code...
Code - C#: [Select]
  1. /// <summary>
  2.         /// Sorts excel input database into a list of DataSet objects
  3.         /// </summary>
  4.         /// <param name="DrawingData">Array returned by ExtractSheetData(DataSet)</param>
  5.         /// <returns>List of DataSet objects</returns>
  6.         private List<DataSet> SortDataSets(object[,] DrawingData, string Method)
  7.         {
  8.             List<DataSet> DeviceData = new List<DataSet>();
  9.             for (int i = 2; i <= DrawingData.GetUpperBound(0); i++)
  10.             {
  11.                 if (Method == "Discreet")
  12.                 {
  13.                     if (DrawingData.GetValue(i, TagNameOS) != null)
  14.                     {
  15.                         DataSet DS = new DataSet();
  16.                         string[] WireTags = new string[8];
  17.                         DS.Channel = DrawingData.GetValue(i, ChannelOS).ToString();
  18.                         DS.TagName = DrawingData.GetValue(i, TagNameOS).ToString();
  19.                         if (DrawingData.GetValue(i, TagNumberOS) != null)
  20.                             DS.TagNumber = DrawingData.GetValue(i, TagNumberOS).ToString();
  21.                         if (DrawingData.GetValue(i, DescripOS) != null)
  22.                             DS.Descriptor1 = DrawingData.GetValue(i, DescripOS).ToString();
  23.                         if (DrawingData.GetValue(i, DescripOS + 1) != null)
  24.                             DS.Descriptor2 = DrawingData.GetValue(i, DescripOS + 1).ToString();
  25.                         if (DrawingData.GetValue(i, DescripOS + 2) != null)
  26.                             DS.Descriptor3 = DrawingData.GetValue(i, DescripOS + 2).ToString();
  27.                         if (DrawingData.GetValue(i, DescripOS + 3) != null)
  28.                             DS.Descriptor4 = DrawingData.GetValue(i, DescripOS + 3).ToString();
  29.                         if (DrawingData.GetValue(i, AreaOS) != null)
  30.                             DS.Area = DrawingData.GetValue(i, AreaOS).ToString();
  31.                         if (DrawingData.GetValue(i, TermTypeOS) != null)
  32.                         {
  33.                             DS.TermType = DrawingData.GetValue(i, TermTypeOS).ToString();
  34.                             if (DrawingData.GetValue(i, Term1OS) != null)
  35.                                 DS.Term1Tag1 = DrawingData.GetValue(i, Term1OS).ToString();
  36.                             if (DrawingData.GetValue(i, Term1OS + 1) != null)
  37.                                 DS.Term1Tag2 = DrawingData.GetValue(i, Term1OS + 1).ToString();
  38.                             if (DrawingData.GetValue(i, Term2OS) != null)
  39.                                 DS.Term2Tag1 = DrawingData.GetValue(i, Term2OS).ToString();
  40.                             if (DrawingData.GetValue(i, Term2OS + 1) != null)
  41.                                 DS.Term2Tag2 = DrawingData.GetValue(i, Term2OS + 1).ToString();
  42.                         }
  43.                         if (DrawingData.GetValue(i, WTYPE12) != null)
  44.                             DS.WType12 = DrawingData.GetValue(i, WTYPE12).ToString();
  45.                         if (DrawingData.GetValue(i, WTYPE34) != null)
  46.                             DS.WType34 = DrawingData.GetValue(i, WTYPE34).ToString();
  47.                         if (DrawingData.GetValue(i, TermNOS) != null)
  48.                             DS.TermNType = DrawingData.GetValue(i, TermNOS).ToString();
  49.                         if (DrawingData.GetValue(i, TermNOS + 1) != null)
  50.                             DS.TermNTag1 = DrawingData.GetValue(i, TermNOS + 1).ToString();
  51.                         if (DrawingData.GetValue(i, TermNOS + 2) != null)
  52.                             DS.TermNTag2 = DrawingData.GetValue(i, TermNOS + 2).ToString();
  53.                         if (DrawingData.GetValue(i, TermNOS) != null)
  54.                             DS.TermFType = DrawingData.GetValue(i, TermFOS).ToString();
  55.                         if (DrawingData.GetValue(i, TermNOS + 1) != null)
  56.                             DS.TermFTag1 = DrawingData.GetValue(i, TermNOS + 1).ToString();
  57.                         if (DrawingData.GetValue(i, TermNOS + 2) != null)
  58.                             DS.TermFTag2 = DrawingData.GetValue(i, TermNOS + 2).ToString();
  59.                         for (int j = 0; j < 8; j++)
  60.                             if (DrawingData.GetValue(i, WireTagsOS + j) != null)
  61.                                 WireTags[j] = DrawingData.GetValue(i, WireTagsOS + j).ToString();
  62.                         DS.WireTags = WireTags;
  63.                         DeviceData.Add(DS);
  64.                     }
  65.                 }
  66.                 if (Method == "Tagging")
  67.                 {
  68.                     if (DrawingData.GetValue(i, TagNameOS) != null)
  69.                     {
  70.                         DataSet DS = new DataSet();
  71.                         DS.Channel = DrawingData.GetValue(i, ChannelOS).ToString();
  72.                         DS.TagName = DrawingData.GetValue(i, TagNameOS).ToString();//TagName holds text alignment
  73.                         DS.TagNumber = DrawingData.GetValue(i, TagNameOS + 1).ToString();//TagNumber holds text height
  74.                         if (DrawingData.GetValue(i, TagNameOS + 2) != null)
  75.                             DS.Descriptor1 = DrawingData.GetValue(i, TagNameOS + 2).ToString();
  76.                         else DS.Descriptor1 = null;
  77.                         if (DrawingData.GetValue(i, TagNameOS + 3) != null)
  78.                             DS.Descriptor2 = DrawingData.GetValue(i, TagNameOS + 3).ToString();
  79.                         else DS.Descriptor2 = null;
  80.                         if (DrawingData.GetValue(i, TagNameOS + 4) != null)
  81.                             DS.Descriptor3 = DrawingData.GetValue(i, TagNameOS + 4).ToString();
  82.                         else DS.Descriptor3 = null;
  83.                         if (DrawingData.GetValue(i, TagNameOS + 5) != null)
  84.                             DS.Descriptor4 = DrawingData.GetValue(i, TagNameOS + 5).ToString();
  85.                         else DS.Descriptor4 = null;
  86.                         if (DrawingData.GetValue(i, TagNameOS + 6) != null) // TermType holds text style
  87.                             DS.TermType = DrawingData.GetValue(i, TagNameOS + 6).ToString();
  88.                         else DS.TermType = null;
  89.                         if (DrawingData.GetValue(i, TagNameOS + 7) != null) // HomeRun holds text direction
  90.                             DS.HomeRun = DrawingData.GetValue(i, TagNameOS + 7).ToString();
  91.                         else DS.HomeRun = null;
  92.                         DeviceData.Add(DS);
  93.                     }
  94.                 }
  95.             }
  96.             return DeviceData;
  97.         }
  98.         /// <summary>
  99.         /// Sorts master dictionary data into individual dictionaries
  100.         /// </summary>
  101.         /// <param name="DictionaryData">Array returned by ExtractSheetData(Dictionary)</param>
  102.         /// <returns>Array of dictionary objects</returns>
  103.         private object[] SortToDictionaries(object[,] DictionaryData)
  104.         {
  105.             Dictionary<string, BlockSet> BlockDictionary = new Dictionary<string, BlockSet>();
  106.             Dictionary<string, AreaSet> AreaDictionary = new Dictionary<string, AreaSet>();
  107.             Dictionary<string, LayerSet> LayerDictionary = new Dictionary<string, LayerSet>();
  108.             Dictionary<string, double> TextDictionary = new Dictionary<string, double>();
  109.             Dictionary<string, AttributeSet> AttributeDictionary = new Dictionary<string, AttributeSet>();
  110.             object[] Dictionaries = new object[5];
  111.             for (int i = 1; i <= DictionaryData.GetUpperBound(0); i++)
  112.             {
  113.  
  114.                 switch (DictionaryData[i, 1] as string)
  115.                 {
  116.                     case "B":
  117.                         BlockSet bs = new BlockSet();
  118.                         bs.GraphName = DictionaryData[i, GraphNmOs].ToString();
  119.                         bs.InsertPt = (double)DictionaryData[i, InsPtOs];
  120.                         bs.Width = (double)DictionaryData[i, WidthOs];
  121.                         bs.Scale = (double)DictionaryData[i, ScaleOS];
  122.                         bs.ExData = DictionaryData[i, ExData] as string;
  123.                         BlockDictionary.Add(DictionaryData[i, ProgNmOs].ToString(), bs);
  124.                         break;
  125.                     case "L":
  126.                         LayerSet ls = new LayerSet();
  127.                         ls.GraphName = DictionaryData[i, GraphNmOs].ToString();
  128.                         LayerDictionary.Add(DictionaryData[i, ProgNmOs].ToString(), ls);
  129.                         break;
  130.                     case "A":
  131.                         AreaSet AreaSet = new AreaSet();
  132.                         AreaSet.JunctionBoxes = DictionaryData[i, JBOS].ToString();
  133.                         AreaDictionary.Add(DictionaryData[i, ProgNmOs].ToString(), AreaSet);
  134.                         break;
  135.                     case "T":
  136.                         TextDictionary.Add(DictionaryData[i, ProgNmOs].ToString(),
  137.                             (double)DictionaryData[i, TextSizeOS]);
  138.                         break;
  139.                     case "AT":
  140.                         AttributeSet AttSet = new AttributeSet();
  141.                         if (DictionaryData[i,ProgNmOs+1]!=null)
  142.                             AttSet.Attribute1 = DictionaryData[i, ProgNmOs + 1].ToString();
  143.                         if (DictionaryData[i, ProgNmOs + 2] != null)
  144.                             AttSet.Attribute2 = DictionaryData[i, ProgNmOs + 2].ToString();
  145.                         if (DictionaryData[i, ProgNmOs + 3] != null)
  146.                             AttSet.Attribute3 = DictionaryData[i, ProgNmOs + 3].ToString();
  147.                         if (DictionaryData[i, ProgNmOs + 4] != null)
  148.                             AttSet.Attribute4 = DictionaryData[i, ProgNmOs + 4].ToString();
  149.                         if (DictionaryData[i, ProgNmOs + 5] != null)
  150.                             AttSet.Attribute5 = DictionaryData[i, ProgNmOs + 5].ToString();
  151.                         AttributeDictionary.Add(
  152.                             DictionaryData[i, ProgNmOs].ToString(), AttSet);
  153.                         break;
  154.                     default:
  155.                         break;
  156.                 }
  157.             }
  158.             Dictionaries[0] = BlockDictionary;
  159.             Dictionaries[1] = LayerDictionary;
  160.             Dictionaries[2] = AreaDictionary;
  161.             Dictionaries[3] = TextDictionary;
  162.             Dictionaries[4] = AttributeDictionary;
  163.             return Dictionaries;
  164.         }
  165.         /// <summary>
  166.         /// Sets text attachment points depending on the dwg layout direction
  167.         /// </summary>
  168.         /// <param name="UnitXVector"></param>
  169.         /// <returns></returns>
  170.         private AttachmentPoint[] GetMtextAttachment(double UnitXVector)
  171.         {
  172.             AttachmentPoint[] attPts = new AttachmentPoint[8];
  173.             if (UnitXVector > 0)
  174.             {
  175.                 attPts[0] = AttachmentPoint.MiddleLeft;
  176.                 attPts[1] = AttachmentPoint.MiddleLeft;
  177.                 attPts[4] = AttachmentPoint.MiddleLeft;
  178.                 attPts[5] = AttachmentPoint.MiddleLeft;
  179.                 attPts[2] = AttachmentPoint.MiddleRight;
  180.                 attPts[3] = AttachmentPoint.MiddleRight;
  181.                 attPts[6] = AttachmentPoint.MiddleRight;
  182.                 attPts[7] = AttachmentPoint.MiddleRight;
  183.             }
  184.             else
  185.             {
  186.                 attPts[0] = AttachmentPoint.MiddleRight;
  187.                 attPts[1] = AttachmentPoint.MiddleRight;
  188.                 attPts[4] = AttachmentPoint.MiddleRight;
  189.                 attPts[5] = AttachmentPoint.MiddleRight;
  190.                 attPts[2] = AttachmentPoint.MiddleLeft;
  191.                 attPts[3] = AttachmentPoint.MiddleLeft;
  192.                 attPts[6] = AttachmentPoint.MiddleLeft;
  193.                 attPts[7] = AttachmentPoint.MiddleLeft;
  194.             }
  195.             return attPts;
  196.         }
  197.         /// <summary>
  198.         /// Calculates MText insertion points for a drawing line
  199.         /// </summary>
  200.         /// <param name="UnitXVector">DWG flow direction</param>
  201.         /// <param name="StdWidth">Terminal Width</param>
  202.         /// <param name="TextHeight">Text Height</param>
  203.         /// <param name="Neutral">Neutral Terminal Position</param>
  204.         /// <param name="Fuse">Fuse Terminal Position</param>
  205.         /// <returns>Array of points</returns>
  206.         private Point3d[] GetMtextInserts(
  207.             double UnitXVector,
  208.             double StdWidth,
  209.             double TextHeight,
  210.             Point3d Neutral,
  211.             Point3d Fuse)
  212.         {
  213.             StdWidth = (StdWidth / 2) + 2;
  214.             Point3d CenterPt = new Point3d(
  215.                 (Fuse.X + Neutral.X) / 2,
  216.                 (Fuse.Y + Neutral.Y) / 2,
  217.                 (Fuse.Z + Neutral.Z) / 2);
  218.  
  219.             Point3d[] insPts = new Point3d[8];
  220.             insPts[0] = new Point3d(Neutral.X + 2 * UnitXVector,
  221.                 Neutral.Y + TextHeight, Neutral.Z);
  222.             insPts[1] = new Point3d(Neutral.X + 2 * UnitXVector,
  223.                 Neutral.Y - TextHeight, Neutral.Z);
  224.             insPts[2] = new Point3d(CenterPt.X - StdWidth * UnitXVector,
  225.                 CenterPt.Y + TextHeight, CenterPt.Z);
  226.             insPts[3] = new Point3d(CenterPt.X - StdWidth * UnitXVector,
  227.                 CenterPt.Y - TextHeight, CenterPt.Z);
  228.             insPts[4] = new Point3d(CenterPt.X + StdWidth * UnitXVector,
  229.                 CenterPt.Y + TextHeight, CenterPt.Z);
  230.             insPts[5] = new Point3d(CenterPt.X + StdWidth * UnitXVector,
  231.                 CenterPt.Y - TextHeight, CenterPt.Z);
  232.             insPts[6] = new Point3d(Fuse.X - 2 * UnitXVector,
  233.                 Fuse.Y + TextHeight, Fuse.Z);
  234.             insPts[7] = new Point3d(Fuse.X - 2 * UnitXVector,
  235.                 Fuse.Y - TextHeight, Fuse.Z);
  236.             return insPts;
  237.         }
  238.     }  
  239. }