Recent Posts

Pages: 1 ... 3 4 [5] 6 7 ... 10
41
External databases / Draw Polylines using Excel
« Last post by fixo on April 20, 2013, 08:28:38 PM »
Here is complete code, tested on AutoCAD 2010, Excel 2010:
Code: [Select]
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
'Imports MyProjectName.MyProjectName

<Assembly: CommandClass(GetType(MyProjectName.MyClassName))>
Namespace MyProjectName
    Public Class MyClassName

        <CommandMethod("hex")> _
        Public Sub ExcelToAcad()
            ReadExcelRange("C:\Test\TestXL.xlsx", "Sheet1")'<-- change file name and sheet name here
        End Sub

        Public Sub ReadExcelRange(ByVal xlFileName As String, ByVal xlSheetName As String)
            Dim lstPlines As New List(Of List(Of Object))
            If System.IO.File.Exists(xlFileName) Then

                Dim xlApp As Excel.Application = Nothing
                Dim xlWorkBooks As Excel.Workbooks = Nothing
                Dim xlWorkBook As Excel.Workbook = Nothing
                Dim xlWorkSheet As Excel.Worksheet = Nothing
                Dim xlWorkSheets As Excel.Sheets = Nothing
                Dim xlCells As Excel.Range = Nothing
                Dim xlRange As Excel.Range = Nothing

                xlApp = New Excel.Application
                Try
                    xlApp.DisplayAlerts = False
                    xlWorkBooks = xlApp.Workbooks
                    xlWorkBook = xlWorkBooks.Open(xlFileName)
                    xlApp.Visible = False
                    xlWorkSheets = xlWorkBook.Sheets
                    xlWorkSheet = CType(xlWorkSheets(xlSheetName), Excel.Worksheet)

                    xlRange = CType(xlWorkSheet.UsedRange.CurrentRegion, Excel.Range)


                    Dim xlRow As Excel.Range = Nothing
                    Dim xlCell As Excel.Range = Nothing

                    For irow As Integer = 1 To xlRange.Rows.Count

                        xlRow = CType(xlRange.Rows(irow), Excel.Range)
                        Dim lstRow As New List(Of Object)
                        For icol As Integer = 1 To xlRow.Cells.Count
                            xlCell = CType(xlRange.Cells(irow, icol), Excel.Range)
                            If xlCell.Value IsNot Nothing Then
                                lstRow.Add(xlCell.Value)
                            End If
                        Next

                        lstPlines.Add(lstRow)

                    Next
                    releaseObject(xlCell)
                    releaseObject(xlRow)
                    releaseObject(xlRange)

                    xlWorkBook.Close()

                    xlApp.Quit()

                    releaseObject(xlWorkSheets)
                    releaseObject(xlWorkSheet)
                    releaseObject(xlWorkBook)
                    releaseObject(xlWorkBooks)
                    releaseObject(xlApp)
                Catch ex As System.Exception

                    System.Windows.MessageBox.Show(ex.Message)

                End Try

                If lstPlines.Count > 0 Then
                    Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                    Dim ed As Editor = doc.Editor
                    Dim db As Database = doc.Database
                    Using tr As Transaction = db.TransactionManager.StartTransaction
                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)

                        For Each lstCoords As List(Of Object) In lstPlines
                            Dim pline As New Polyline
                            Dim n As Integer = 0
                            Dim i As Integer = 0
                            For n = 0 To lstCoords.Count - 1 Step 2
                                Dim x As Double = Convert.ToDouble(lstCoords(n))
                                Dim y As Double = Convert.ToDouble(lstCoords(n + 1))

                                Dim pp As Point2d = New Point2d(x, y)
                                pline.AddVertexAt(i, pp, 0, 0, 0)
                                i += 1
                            Next
                            btr.AppendEntity(pline)
                            tr.AddNewlyCreatedDBObject(pline, True)
                        Next
                        tr.Commit()
                      System.Windows.MessageBox.Show("See result.")
                    End Using
                End If
            Else
                System.Windows.MessageBox.Show("'" & xlFileName & "' does not found.")
            End If
        End Sub
        Public Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
                obj = Nothing
            Catch ex As System.Exception
                System.Diagnostics.Debug.Print(ex.ToString())
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub
End Namespace

Please, see about Excel VB.NET for more :
http://www.siddharthrout.com/tag/vb-net-and-excel/
http://code.msdn.microsoft.com/Basics-of-using-Excel-4453945d
In the attached picture is data used for this code
42
This site / Re: Welcome on the AutoCAD .NET developpers forums
« Last post by skillpeak on April 15, 2013, 06:01:32 AM »
hi, this is skillpeak.
I am a beginner in autocad.
43
Events / Re: EventHandler for Entity Selection
« Last post by BlackBox on April 09, 2013, 09:18:35 PM »
I´m trying to get all entites (ids...) which are currently selected for displaying xdatas
in a palette.
If I select for example lines I want the ObjectIds saved in a Collection or array. If I unselect
the Entity/ies I want to remove the Id from the Collection.
If I run a command or something else, the Entites will be unselected too, but with the Method
ed.SelectionRemoved the entite Ids wont be removed / the event isnt fired.

And I dont want to run a command, this should be "interactive" like the autocad property palette...

This is an older thread, so not sure if you already got it sorted, but you might consider the Document.ImpliedSelectionChanged Event.
44
3D entities / Re: AutoCAD 2011 & VB.net Express 2008
« Last post by NirantarVidyarthee on April 04, 2013, 08:46:26 PM »
Thank you.

I'll try it and see if this works.
45
3D entities / Re: AutoCAD 2011 & VB.net Express 2008
« Last post by dockee on April 03, 2013, 07:40:12 PM »
I think the problem come from security framework.

See capsol command to make authorization on worker pc.

46
3D entities / Deployment - VB.Net Express 2008
« Last post by NirantarVidyarthee on April 02, 2013, 08:40:44 PM »
I have developed an Application using VB.Net Express 2008.

But I can't create a setup for this application. I use the publish command as suggested by Microsift. I am able to create a setup.exe but when I dblclick it it starts the application itself instead of asking where to install the application and then ends with an error.

Can any body please guide me the correct way to create the setup?

I can provide more information if you need.
47
3D entities / AutoCAD 2011 & VB.net Express 2008
« Last post by NirantarVidyarthee on April 02, 2013, 08:36:34 PM »
I have developed an pit-of-process Application using VB.Net Express 2008 and AutoCAD 2011. (These are User requirements and hence can't be changed).

The application developed runs on the development computer without any problem. But it does not run on the working computer. Details are as below:

Development PC is a single user stand-alone PC while working PC is a corporate network node. Operating systems, AutoCAD versions and system variables on both PCs match.

The application runs on development PC without any problems. However, on the working node it runs only through administrative access (login) but not with the user login.

Can any body please suggest me what needs to be done so that the program runs with user access (not Admin).

I can provide more details if you need.
48
Hatching / Re: Create associative hatch
« Last post by bcinnv on March 26, 2013, 01:21:28 PM »
Thanks fixo!  I'm tired of using the built in hatch functions... its automatic selection almost always crashes my session.
49
Blocks / Numbering multileader with user block content
« Last post by fixo on March 24, 2013, 10:38:52 PM »
       
In this code used multileader with attributed block "Callout Bubble - Imperial",
after this block is inserted, then deleted, we can create multileader with block "_DetailCallout".
Then we can to number multileader this way:
Code: [Select]
         [CommandMethod("mab")]
        public static void MleaderBlockNumbering()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            Transaction tr = db.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord;
                    string prefix = ""; string suffix = ""; string atag = "SHEETNUMBER";// used "_DetailCallout" block fro Annotation palette
                    int start; int step; bool first = true;
                    ObjectId attnum = ObjectId.Null;
                    PromptStringOptions psto = new PromptStringOptions("\nPrefix: ");
                    psto.AllowSpaces = true;
                    PromptResult res;
                    res = ed.GetString(psto);
                    if (res.Status != PromptStatus.None)
                        prefix = res.StringResult;
                    psto = new PromptStringOptions("\nSuffix: ");
                    psto.AllowSpaces = true;
                    res = ed.GetString(psto);
                    if (res.Status != PromptStatus.None)
                        suffix = res.StringResult;
                    PromptIntegerOptions pio = new PromptIntegerOptions("");
                    pio.Message = "\nStarting number: ";
                    // Restrict input to positive and non-negative values
                    pio.AllowZero = false;
                    pio.AllowNegative = false;
                    pio.DefaultValue = 1;
                    PromptIntegerResult pires = ed.GetInteger(pio);
                    if (pires.Status != PromptStatus.OK) return;
                    start = pires.Value;
                    pio = new PromptIntegerOptions("");
                    pio.Message = "\nIncrement step: ";
                    // Restrict input to positive and non-negative values
                    pio.AllowZero = false;
                    pio.AllowNegative = false;
                    pio.DefaultValue = 1;
                    pires = ed.GetInteger(pio);
                    if (pires.Status != PromptStatus.OK) return;
                    step = pires.Value;
                    doc.TransactionManager.EnableGraphicsFlush(true);
                    //------------------------------------------------------------------------------------------
                    while (true)
                    {
                        PromptSelectionOptions pso = new PromptSelectionOptions();
                        pso.MessageForRemoval = "\nWrong object type or multileader with no block selected";
                        pso.MessageForAdding = "\nSelect multileader with block: ";
                        pso.SingleOnly = true;
                        pso.SinglePickInSpace = true;
                        TypedValue[] tvs = new TypedValue[] {
                new TypedValue((int) DxfCode.Operator, "<AND"),
                new TypedValue((int) DxfCode.Start, "multileader"),
                new TypedValue((int)  DxfCode.Operator, "<NOT"),
                new TypedValue((int)  DxfCode.Operator, "<OR"),
                new TypedValue(296, 0), new TypedValue(296, 1),
                new TypedValue((int)  DxfCode.Operator, "OR>"),
                new TypedValue((int) DxfCode.Operator, "NOT>"),
                new TypedValue((int) DxfCode .Operator, "AND>")
            };

                        SelectionFilter sf = new SelectionFilter(tvs);

                        PromptSelectionResult psr = ed.GetSelection(pso, sf);

                        if (psr.Status != PromptStatus.OK)
                        {
                            ed.WriteMessage("\nWrong selection");
                            break;

                        }

                        Entity ent;
                        ObjectId id = ObjectId.Null;
                        SelectionSet sset = psr.Value;
                        ObjectId[] ids = sset.GetObjectIds();
                        id = ids[0];
                        ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
                        MLeader mlead = ent as MLeader;
                        if (mlead == null) return;
                        mlead.UpgradeOpen();
                        if (first)
                        {
                            ObjectId blkid = mlead.BlockContentId;
                            BlockTableRecord mbtr = (BlockTableRecord)tr.GetObject(blkid, OpenMode.ForRead, false) as BlockTableRecord;

                            foreach (ObjectId attid in mbtr)
                            {
                                DBObject obj = tr.GetObject(attid, OpenMode.ForRead);
                                if (obj is AttributeDefinition)
                                {
                                    AttributeDefinition attdef = obj as AttributeDefinition;
                                    if (attdef.Tag == atag)
                                    {
                                        attnum = attdef.ObjectId;
                                        break;
                                    }
                                }
                            }
                            first = false;
                        }
                        AttributeReference attref = mlead.GetBlockAttribute(attnum);
                        attref.TextString = prefix + start.ToString() + suffix;
                        mlead.SetBlockAttribute(attnum, attref);
                        start = start + step;
                        tr.TransactionManager.QueueForGraphicsFlush();
                    }
                    doc.TransactionManager.FlushGraphics();
                    tr.Commit();
                    ed.UpdateScreen();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {

            }
        }
50
Windows forms / Re: Use dinamically created form in the project
« Last post by fixo on March 23, 2013, 05:26:13 PM »
You're welcome
Cheers :)
Pages: 1 ... 3 4 [5] 6 7 ... 10