Author Topic: Using VB.Net to Import/Insert a DXF File in Existing Drawing  (Read 28669 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #45 on: March 27, 2011, 03:57:22 PM »
Or better yet to work on empty and non-empty drawings

Leave the if statement  and move
Code: [Select]
       Dim lastEnt As ObjectId = AcUtils.EntLast()
up to the top of your code

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #46 on: March 27, 2011, 04:50:49 PM »
I was also making a assumption in last post about EntFirst()

I am not fimilar with Lisp and do not know if EntFirst() acts just like Lisp version, but

does EntFirst() get first entity added to Database in the current Session?

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #47 on: March 28, 2011, 03:08:55 AM »
I was also making a assumption in last post about EntFirst()

I am not fimilar with Lisp and do not know if EntFirst() acts just like Lisp version, but

does EntFirst() get first entity added to Database in the current Session?


I think so because while debugging it loops through all the new inserted objects.

Thanks Jeff for your answers, I'll have a test this evening when I'm home.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #48 on: March 28, 2011, 05:04:22 AM »
A couple of options to play with ...
"InsDXF7"  Collect the  imported entitys then itterate the collection to change Color
"InsDXF8"  Change Color While iterating the imported entitys .. no collection
"InsDXF9"  Change Color in the temporary Side DataBase
Code: [Select]

        [CommandMethod("InsDXF7")]
        public void InsDXF7()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Database tmpDb = new Database(false, true);

            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log");
            Point3d pnt1 = new Point3d(0, 0, 0);
            Point3d pnt2 = new Point3d(1000, 1000, 0);
            double scale = 10.0;
            Vector3d vector = new Vector3d(0, 0, 0);

            Matrix3d transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2));
            ObjectId lastEnt = AcUtils.EntLast();
            Autodesk.AutoCAD.Colors.Color newColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1);
            //
            db.Insert(transform, tmpDb, true);
            //
            using( Transaction tr = doc.TransactionManager.StartTransaction() ) {
                // if the db was empty lastent will be null        
                try {
                    if( lastEnt == ObjectId.Null ) {
                        lastEnt = AcUtils.EntFirst();
                        idColl.Add(lastEnt);
                    }
                    ObjectId nextent = AcUtils.EntNext(lastEnt);
                    while( nextent != ObjectId.Null ) {
                        idColl.Add(nextent);
                        nextent = AcUtils.EntNext(nextent);
                    }

                    foreach( ObjectId id in idColl ) {
                        Entity acEntity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        acEntity.Color = newColor;
                    }
                } catch( Autodesk.AutoCAD.Runtime.Exception exx ) {
                    ed.WriteMessage("\n" + exx.ToString());
                }
                tr.Commit();
            }
        }

Code: [Select]

        [CommandMethod("InsDXF8")]
        public void InsDXF8()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Database tmpDb = new Database(false, true);

            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log");
            Point3d pnt1 = new Point3d(0, 0, 0);
            Point3d pnt2 = new Point3d(1000, 1000, 0);
            double scale = 10.0;
            Vector3d vector = new Vector3d(0, 0, 0);

            Matrix3d transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2));
            ObjectIdCollection idColl = new ObjectIdCollection();
            ObjectId lastEnt = AcUtils.EntLast();
            Entity acEntity;
            Autodesk.AutoCAD.Colors.Color newColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1);
            //
            db.Insert(transform, tmpDb, true);
            //
            using( Transaction tr = doc.TransactionManager.StartTransaction() ) {
                // if the db was empty lastent will be null  
                try {
                    if( lastEnt == ObjectId.Null ) {
                        lastEnt = AcUtils.EntFirst();
                        acEntity = (Entity)tr.GetObject(lastEnt, OpenMode.ForWrite);
                        acEntity.Color = newColor;
                    }
                    ObjectId nextent = AcUtils.EntNext(lastEnt);

                    while( nextent != ObjectId.Null ) {
                        acEntity = (Entity)tr.GetObject(nextent, OpenMode.ForWrite);
                        acEntity.Color = newColor;
                        nextent = AcUtils.EntNext(nextent);
                    }
                } catch( Autodesk.AutoCAD.Runtime.Exception exx ) {
                    ed.WriteMessage("\n" + exx.ToString());
                }
                tr.Commit();
            }
        }

Code: [Select]

        [CommandMethod("InsDXF9")]
        public void InsDXF9()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            Database tmpDb = new Database(false, true);

            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log");
            Point3d pnt1 = new Point3d(0, 0, 0);
            Point3d pnt2 = new Point3d(1000, 1000, 0);
            double scale = 10.0;
            Vector3d vector = new Vector3d(0, 0, 0);

            Matrix3d transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2));
            ObjectIdCollection idColl = new ObjectIdCollection();
            Entity acEntity;
            Autodesk.AutoCAD.Colors.Color newColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1);
            //
            using( Transaction tr = tmpDb.TransactionManager.StartTransaction() ) {
                try {
                    BlockTable tmpDbBlockTable = (BlockTable)
                     tr.GetObject(tmpDb.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord tmpDbModelSpace = (BlockTableRecord)
                            tr.GetObject(tmpDbBlockTable[ BlockTableRecord.ModelSpace ], OpenMode.ForWrite);

                    foreach( ObjectId id in tmpDbModelSpace ) {
                        acEntity = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        acEntity.Color = newColor;
                    }
                    tr.Commit();
                } catch( Autodesk.AutoCAD.Runtime.Exception exx ) {
                    ed.WriteMessage("\n" + exx.ToString());
                }
            }
            //
            db.Insert(transform, tmpDb, true);

        }
« Last Edit: March 28, 2011, 05:12:39 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #49 on: March 28, 2011, 02:32:32 PM »
Thanks Kerry for your code!  :-)  I've tried all three and I only have success if I change the objects in the temporary database, before inserting in the current drawing. I'm not sure what is wrong with the other examples, I've read the instructions of Jeff but the code does not do anything. I don't get errors, there are no exceptions, and while debugging it seems the objects are there, but not available.

But the last example does work very well and the options are endless now to alter the objects before inserting! Thanks!!!  :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #50 on: March 28, 2011, 03:07:47 PM »

huiz,

I probably don't need to say this, but .. the 3 examples work for me in c# from vs2010Pro into AC2011.

I'd guess that your translation may be skewey.

Can you post the VB.net code you used for each or post he solution zipped so I can test it here.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #51 on: March 29, 2011, 02:29:24 AM »
I thought they did work in C# but unfortunately not in vb. Here are the examples in vb:

Code: [Select]
<CommandMethod("InsDXF7")> _
Public Sub InsDXF7()
Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim tmpDb As New Database(False, True)

tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
Dim pnt1 As New Point3d(0, 0, 0)
Dim pnt2 As New Point3d(1000, 1000, 0)
Dim scale As Double = 10.0
Dim vector As New Vector3d(0, 0, 0)

Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
Dim lastEnt As ObjectId = AcUtils.EntLast()
Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

db.Insert(transform, tmpDb, True)

Using tr As Transaction = doc.TransactionManager.StartTransaction()
' if the db was empty lastent will be null       
Try
If lastEnt = ObjectId.Null Then
lastEnt = AcUtils.EntFirst()
idColl.Add(lastEnt)
End If
Dim nextent As ObjectId = AcUtils.EntNext(lastEnt)
While nextent <> ObjectId.Null
idColl.Add(nextent)
nextent = AcUtils.EntNext(nextent)
End While

For Each id As ObjectId In idColl
Dim acEntity As Entity = DirectCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
acEntity.Color = newColor
Next
Catch exx As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbLf & exx.ToString())
End Try
tr.Commit()
End Using
End Sub

Code: [Select]
<CommandMethod("InsDXF8")> _
Public Sub InsDXF8()
Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim tmpDb As New Database(False, True)

tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
Dim pnt1 As New Point3d(0, 0, 0)
Dim pnt2 As New Point3d(1000, 1000, 0)
Dim scale As Double = 10.0
Dim vector As New Vector3d(0, 0, 0)

Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
Dim idColl As New ObjectIdCollection()
Dim lastEnt As ObjectId = AcUtils.EntLast()
Dim acEntity As Entity
Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

db.Insert(transform, tmpDb, True)

Using tr As Transaction = doc.TransactionManager.StartTransaction()
' if the db was empty lastent will be null   
Try
If lastEnt = ObjectId.Null Then
lastEnt = AcUtils.EntFirst()
acEntity = DirectCast(tr.GetObject(lastEnt, OpenMode.ForWrite), Entity)
acEntity.Color = newColor
End If
Dim nextent As ObjectId = AcUtils.EntNext(lastEnt)

While nextent <> ObjectId.Null
acEntity = DirectCast(tr.GetObject(nextent, OpenMode.ForWrite), Entity)
acEntity.Color = newColor
nextent = AcUtils.EntNext(nextent)
End While
Catch exx As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbLf & exx.ToString())
End Try
tr.Commit()
End Using
End Sub

Code: [Select]
<CommandMethod("InsDXF9")> _
Public Sub InsDXF9()
Dim doc As Document = AcadApp.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
Dim tmpDb As New Database(False, True)

tmpDb.DxfIn("K:\ToTest\Squircle.dxf", "K:\ToTest\Squircle.log")
Dim pnt1 As New Point3d(0, 0, 0)
Dim pnt2 As New Point3d(1000, 1000, 0)
Dim scale As Double = 10.0
Dim vector As New Vector3d(0, 0, 0)

Dim transform As Matrix3d = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))
Dim idColl As New ObjectIdCollection()
Dim acEntity As Entity
Dim newColor As Autodesk.AutoCAD.Colors.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

Using tr As Transaction = tmpDb.TransactionManager.StartTransaction()
Try
Dim tmpDbBlockTable As BlockTable = DirectCast(tr.GetObject(tmpDb.BlockTableId, OpenMode.ForRead), BlockTable)
Dim tmpDbModelSpace As BlockTableRecord = DirectCast(tr.GetObject(tmpDbBlockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

For Each id As ObjectId In tmpDbModelSpace
acEntity = DirectCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
acEntity.Color = newColor
Next
tr.Commit()
Catch exx As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbLf & exx.ToString())
End Try
End Using

db.Insert(transform, tmpDb, True)

End Sub
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #52 on: March 29, 2011, 03:35:49 AM »
Give these a run ...
(Code solution .ZIP attached)



Code: [Select]

Option Explicit On
Option Strict On

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Colors
Imports Utils = Autodesk.AutoCAD.Internal.Utils

<Assembly: CommandClass(GetType(InsertDXF.MyCommands))>

Namespace InsertDXF

    Public Class MyCommands


   '' < snip >

    End Class

End Namespace

Code: [Select]

        <CommandMethod("InsDXF7")> _
        Public Sub InsDXF7()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\ToTest\\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim idColl As New ObjectIdCollection
            Dim lastEnt As ObjectId = Utils.EntLast
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            db.Insert(transform, tmpDb, True)
            Using tr As Transaction = doc.TransactionManager.StartTransaction
                Try
                    If (lastEnt = ObjectId.Null) Then
                        lastEnt = Utils.EntFirst
                        idColl.Add(lastEnt)
                    End If

                    Dim nextent As ObjectId = Utils.EntNext(lastEnt)
                    Do While (nextent <> ObjectId.Null)
                        idColl.Add(nextent)
                        nextent = Utils.EntNext(nextent)
                    Loop

                    Dim id As ObjectId
                    For Each id In idColl
                        Dim acEntity As Entity = DirectCast(tr.GetObject( _
                                id, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                    Next

                Catch exx As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage((ChrW(10) & exx.ToString))
                End Try
                tr.Commit()
            End Using
        End Sub


Code: [Select]

        <CommandMethod("InsDXF8")> _
        Public Sub InsDXF8()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim idColl As New ObjectIdCollection
            Dim lastEnt As ObjectId = Utils.EntLast
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            db.Insert(transform, tmpDb, True)
            Using tr As Transaction = doc.TransactionManager.StartTransaction
                Try
                    Dim acEntity As Entity
                    If (lastEnt = ObjectId.Null) Then
                        lastEnt = Utils.EntFirst
                        acEntity = DirectCast(tr.GetObject( _
                                lastEnt, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                    End If

                    Dim nextent As ObjectId = Utils.EntNext(lastEnt)
                    Do While (nextent <> ObjectId.Null)
                        acEntity = DirectCast(tr.GetObject( _
                                nextent, OpenMode.ForWrite), Entity)
                        acEntity.Color = newColor
                        nextent = Utils.EntNext(nextent)
                    Loop
                Catch exx As Autodesk.AutoCAD.Runtime.Exception
                    ed.WriteMessage((ChrW(10) & exx.ToString))
                End Try
                tr.Commit()
            End Using
        End Sub

Code: [Select]

       <CommandMethod("InsDXF9")> _
        Public Sub InsDXF9()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tmpDb As New Database(False, True)
            tmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\Squircle.log")
            Dim pnt1 As New Point3d(0, 0, 0)
            Dim pnt2 As New Point3d(1000, 1000, 0)
            Dim scale As Double = 10
            Dim vector As New Vector3d(0, 0, 0)
            Dim transform As Matrix3d = (Matrix3d.Scaling(scale, pnt2) _
                                         * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2)))
            Dim newColor As Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
            Dim tr As Transaction = tmpDb.TransactionManager.StartTransaction
            Try
                Dim tmpDbBlockTable As BlockTable = DirectCast(tr.GetObject( _
                        tmpDb.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim tmpDbModelSpace As BlockTableRecord = DirectCast(tr.GetObject( _
                        tmpDbBlockTable.Item(BlockTableRecord.ModelSpace), _
                        OpenMode.ForWrite), BlockTableRecord)

                Dim id As ObjectId
                For Each id In tmpDbModelSpace
                    Dim acEntity As Entity = DirectCast(tr.GetObject( _
                            id, OpenMode.ForWrite), Entity)
                    acEntity.Color = newColor
                Next

                tr.Commit()
            Catch exx As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage((ChrW(10) & exx.ToString))
            Finally
                If (Not tr Is Nothing) Then
                    tr.Dispose()
                End If
            End Try
            db.Insert(transform, tmpDb, True)
        End Sub
« Last Edit: March 29, 2011, 03:51:28 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

kaefer

  • Guest
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #53 on: March 29, 2011, 04:18:43 AM »
Code: [Select]
Option Explicit On
Option Strict On

Does the culprit sit right here? 

Cheers, Thorsten (missing bargepole, won't do VB)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #54 on: March 29, 2011, 04:23:53 AM »
Code: [Select]
Option Explicit On
Option Strict On

Does the culprit sit right here? 

Cheers, Thorsten (missing bargepole, won't do VB)

Don't know Thorsten, but it didn't seen correct to me to code with them off .... but that's probably just me :)

I've decided I need to at least be able to read VB and write a little ... as much as I hate the Dim word  :-P
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

huiz

  • Swamp Rat
  • Posts: 913
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #55 on: March 29, 2011, 02:53:37 PM »
[..]
I've decided I need to at least be able to read VB and write a little ... as much as I hate the Dim word  :-P

All three procedures do work now Kerry. I'll have a closer look what made the difference, as curious as I am. Btw, I've checked the time they need and changing the objects in the temp database is much faster. About half of the time the others need. So I stick with the last piece of code :-)

Btw, I am able to read C# but can't write it, though I don't have trouble with PHP.  Thanks again for your help :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.