.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

import Excel layer list to AutoCAD

2 REPLIES 2
Reply
Message 1 of 3
wang890
4052 Views, 2 Replies

import Excel layer list to AutoCAD

hi everyone

 

has anybody made a program and can share it here? both source code in vb.net or dll file is fine.

 

thank you very much.

Stantec
Dell Precision 5530, Prism M320PU, C3D 14/17/19
2 REPLIES 2
Message 2 of 3
Hallex
in reply to: wang890

Take a look ath this example

http://through-the-interface.typepad.com/through_the_interface/2007/08/creating-an-aut.html

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 3
Anonymous
in reply to: wang890

Hi there,

 

I've created a simple project to read the layernames from excel and add these to the drawing.

 

It's written in c# But there are enough converters around. Project is attached including an excel 2007 file.

 

Good luck,

 

Irvin

 

using System;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

using Excel = Microsoft.Office.Interop.Excel;

namespace CreateLayerFromExcel
{
    public class Layer
    {
        [CommandMethod("CreatLayersFromExcel")]
        public void CreatLayersFromExcel()
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            string str;
            int rCnt = 0;
            int cCnt = 0;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("c:\\temp\\test.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    CreateLayer(str);
                    System.Windows.Forms.MessageBox.Show(str + " created!");
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (System.Exception ex)
            {
                obj = null;
                System.Windows.Forms.MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void CreateLayer(string layerName)
        {
            ObjectId layerId;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (lt.Has(layerName))
                {
                    layerId = lt[layerName];
                }
                else
                {
                    LayerTableRecord ltr = new LayerTableRecord();
                    ltr.Name = layerName;
                    //ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);

                    lt.UpgradeOpen();
                    layerId = lt.Add(ltr);
                    trans.AddNewlyCreatedDBObject(ltr, true);
                }
                trans.Commit();
            }
        }
    }
}

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost