DataSet to string w/ C# and AutoCAD .net API -


i have small group of functions draws schematic in autocad using c#.

what want load excel spreadsheet in autocad, automatically draw schematic , fill out autocad attributes excel spreadsheet (pretty tricky).

i think problem getting data dataset , incorporating table.rows[i + 0] (in drawtest() function) or adding proper code commandmethod("circuitarray") function, particularly attref.textstring = "hello world"; part. if add text in between "" on attref.textstring = "hello world"; part place same text autocad attributes of schematic.

spreadsheet example:

spread sheet example

circuit example:

circuit example

public class mycommands {         [commandmethod("drawtest")]     public void drawtest()     {         string path = selectspreadsheet();         system.data.datatable table = readexceltotable(path);         insertblocks();         addiomodule("ai-1756-if16h-sheet1");         (int = 0;  < 2; i++)         {             circuitarray("ai-ait-circuit", 25, 17-i, 0, table.rows[i + 0]);         }      }    [commandmethod("loadiolist")]     static public string selectspreadsheet()     {         document doc =           application.documentmanager.mdiactivedocument;         database db = doc.database;         editor ed = doc.editor;         openfiledialog ofd =           new openfiledialog(             "select excel spreadsheet link",             null,             "xls; xlsx",             "excelfiletolink",             openfiledialog.openfiledialogflags.donottransferremotefiles           );         system.windows.forms.dialogresult dr =           ofd.showdialog();         if (dr != system.windows.forms.dialogresult.ok)             return null;         ed.writemessage(           "\nfile selected \"{0}\".",           ofd.filename         );         return ofd.filename;      }      public static system.data.datatable readexceltotable(string path)     {          string connstring = "provider=microsoft.ace.oledb.12.0;data source=" + path + ";extended properties='excel 8.0;hdr=no;imex=1';";         system.data.dataset set = new dataset();         using (oledbconnection conn = new oledbconnection(connstring))         {             conn.open();             system.data.datatable sheetsname = conn.getoledbschematable(oledbschemaguid.tables, new object[] { null, null, null, "table" });             string firstsheetname = sheetsname.rows[0][2].tostring();             string sql = string.format("select * [{0}]", firstsheetname);             oledbdataadapter ada = new oledbdataadapter(sql, connstring);             ada.fill(set);             conn.close();         }         return set.tables[0];                }   [commandmethod("insertblocks")]     public void insertblocks() // method can have name     {         document doc = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument;         using (database opendb = new database(false, true))         {             opendb.readdwgfile("c:\\users\\siewertb\\documents\\cad\\cadautomation\\io-schematics.dwg",                 system.io.fileshare.readwrite, true, "");              objectidcollection ids = new objectidcollection();             using (transaction tr =                     opendb.transactionmanager.starttransaction())             {                 //for example, block name                 blocktable bt;                 bt = (blocktable)tr.getobject(opendb.blocktableid                                                , openmode.forread);                   if (bt.has("ai-1756-if16h-sheet1"))                 {                     ids.add(bt["ai-1756-if16h-sheet1"]);                 }                  if (bt.has("ai-ait-circuit"))                 {                     ids.add(bt["ai-ait-circuit"]);                 }                  tr.commit();             }              //if found, add block             if (ids.count != 0)             {                 //get current drawing database                 database destdb = doc.database;                  idmapping imap = new idmapping();                 destdb.wblockcloneobjects(ids, destdb.blocktableid                        , imap, duplicaterecordcloning.ignore, false);             }         }     }  [commandmethod("addiomodule")]     static public void addiomodule(string name)     {         database db = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.database;         using (transaction myt = db.transactionmanager.starttransaction())         {             //get block definition             string blockname = name;             blocktable bt =                 db.blocktableid.getobject(openmode.forread) blocktable;             blocktablerecord blockdef =               bt[blockname].getobject(openmode.forread) blocktablerecord;             //also open paper space - we'll adding our blockreference             blocktablerecord ps =               bt[blocktablerecord.paperspace].getobject(openmode.forwrite)                                                       blocktablerecord;             //create new blockreference, , link our block definition             point3d point = new point3d(32.03125, 20.34375, 0);             using (blockreference blockref =                     new blockreference(point, blockdef.objectid))             {                 //add block reference paper space                 ps.appendentity(blockref);                 myt.addnewlycreateddbobject(blockref, true);                 //iterate block definition find non-constant                 // attributedefinitions              }             //our work here done             myt.commit();         }     }  [commandmethod("addcircuit")]     static public void addcircuit(string name)     {         database db = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.database;         using (transaction myt = db.transactionmanager.starttransaction())         {             //get block definition             string blockname = name;             blocktable bt =                 db.blocktableid.getobject(openmode.forread) blocktable;             blocktablerecord blockdef =               bt[blockname].getobject(openmode.forread) blocktablerecord;             //also open paper space - we'll adding our blockreference             blocktablerecord ps =               bt[blocktablerecord.paperspace].getobject(openmode.forwrite)                                                       blocktablerecord;             //create new blockreference, , link our block definition             point3d point = new point3d(25, 17.28125000, 0);             using (blockreference blockref =                     new blockreference(point, blockdef.objectid))             {                 //add block reference modelspace                 ps.appendentity(blockref);                 myt.addnewlycreateddbobject(blockref, true);                 //iterate block definition find non-constant                 // attributedefinitions                 foreach (objectid id in blockdef)                 {                     dbobject obj = id.getobject(openmode.forread);                     attributedefinition attdef = obj attributedefinition;                  }             }             //our work here done             myt.commit();         }  [commandmethod("circuitarray")]     public void circuitarray(string name, int x, int y, int z, datarow row)         {         database db = autodesk.autocad.applicationservices.application.documentmanager.mdiactivedocument.database;         using (transaction myt = db.transactionmanager.starttransaction())         {             //get block definition             string blockname = name;             blocktable bt =                 db.blocktableid.getobject(openmode.forread) blocktable;             blocktablerecord blockdef =               bt[blockname].getobject(openmode.forread) blocktablerecord;             //also open paper space - we'll adding our blockreference             blocktablerecord ps =               bt[blocktablerecord.paperspace].getobject(openmode.forwrite)                                                       blocktablerecord;             //create new blockreference, , link our block definition             point3d point = new point3d(x, y, z);              using (blockreference blockref =                     new blockreference(point, blockdef.objectid))             {                 //add block reference paper space                 ps.appendentity(blockref);                 myt.addnewlycreateddbobject(blockref, true);                  //iterate block definition find non-constant                 // attributedefinitions                   int count = 0;                 foreach (objectid id in blockdef)                 {                     count++;                     dbobject obj = id.getobject(openmode.forread);                     attributedefinition attdef = obj attributedefinition;                     if ((attdef != null) && (!attdef.constant))                     {                         //this non-constant attributedefinition                         //create new attributereference                         using (attributereference attref = new attributereference())                         {                             attref.setattributefromblock(attdef, blockref.blocktransform);                             attref.textstring =  "hello world";                             //add attributereference blockreference                             blockref.attributecollection.appendattribute(attref);                          }                     }                 }             }             //our work here done             myt.commit();         } 


Comments