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

0 Members and 1 Guest are viewing this topic.

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 #30 on: March 17, 2011, 10:08:54 AM »
Database.DxfIn
I'm blind as well as stupid !!
Thanks Philippe at ADN.

Code: [Select]

        [CommandMethod("netInsertDxf")]

        static public void netInsertDxf()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Database TmpDb = new Database(false, true);

            TmpDb.DxfIn("K:\\ToTest\\Squircle.dxf", "K:\\ToTest\\Squircle.log");

            Matrix3d Transform = Matrix3d.Identity;
            db.Insert(Transform, TmpDb, true);
        }

and I just noticed that kaefer was using it in Post Reply #24
« Last Edit: March 17, 2011, 10:19:49 AM by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #31 on: March 17, 2011, 12:14:08 PM »
...at my suggestion from post reply #14
Me

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 #32 on: March 17, 2011, 02:49:58 PM »
...at my suggestion from post reply #14

Glenn,
At the time I read that as  “AcadDocument.Import” method on the DXF file into a side db. ... which I know does work.
« Last Edit: March 17, 2011, 03:02:27 PM by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #33 on: March 17, 2011, 04:45:48 PM »
Apologies Kerry - I just re-read what I posted and it might have come across as a bit...terse - not my intention. Also, I wouldn't suggest the use of that...nasty COM stuff :evil:, unless absolutely necessary  :-D
Me

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 #34 on: March 17, 2011, 05:07:15 PM »


No pain for me ... except that I completely missed the reference ... that's just embarrassing  :oops:
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #35 on: March 22, 2011, 05:40:01 AM »
I have tried some things with importing dxf files but I cannot continue. There is not many information about this subject to find.

This is the code till now (thanks to Kerry):

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry

Public Class clsImportDXF

  <CommandMethod("InsertDxf")> _
  Public Sub InsertDxf()
    Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    Dim TmpDb As Database = New Database(False, True)
    TmpDb.DxfIn("G:\\test.dxf", "G:\\test.log")
    Dim pnt1 As New Point3d(0, 0, 0)
    Dim pnt2 As New Point3d(1000, 1000, 0)

    Dim Transform As Matrix3d = Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))

    db.Insert(Transform, TmpDb, True)
  End Sub

End Class

This code does insert a dxf and moves it to a new location. But I also want to scale all entities. How can I add a scale in this code? Can I extend Transform with a Scale?

Further I want to select all entities from the dxf and place them on another layer. How do I select all entities?
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.

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #36 on: March 22, 2011, 05:58:03 AM »
If I combine Matrix3D options, then they are ignored:

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry

Public Class clsImportDXF

  <CommandMethod("InsertDxf")> _
  Public Sub InsertDxf()
    Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    Dim TmpDb As Database = New Database(False, True)
    TmpDb.DxfIn("G:\\test.dxf", "G:\\test.log")
    Dim pnt1 As New Point3d(0, 0, 0)
    Dim pnt2 As New Point3d(1000, 1000, 0)
    Dim scale As Double = 10

    Dim Transform As New Matrix3d
    Transform = Matrix3d.Scaling(scale, pnt1)
    Transform = Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))

    db.Insert(Transform, TmpDb, True)
  End Sub

End Class
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 #37 on: March 22, 2011, 06:26:00 AM »
I think you will need to Multiply the matrix's together ...

WAG

               Dim Transform As New Matrix3d
                Transform = Matrix3d.Scaling(scale, pnt1) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))

added :
This works in C# anyway :)
Code: [Select]
            Matrix3d Transform = Matrix3d.Identity;
            Transform = Transform * Matrix3d.Scaling(5.0, Point3d.Origin);
« Last Edit: March 22, 2011, 06:35:56 AM by Kerry »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #38 on: March 22, 2011, 06:39:48 AM »
Cool! I didn't know that this was possible :-) How is this method called?

I have now:

Code: [Select]
    Dim Transform As New Matrix3d
    Transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))

I had to use the displacement point as scale location (pnt2) instead of the origin.



Btw, how do I get all placed entities so I can change the objects layer? Or should I iterate all objects in the TmpDB first?
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.

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #39 on: March 22, 2011, 06:53:51 AM »
Btw, how do I get all placed entities so I can change the objects layer? Or should I iterate all objects in the TmpDB first?

You could read further upthread:
ObjectAppended event approach http://www.theswamp.org/index.php?topic=37409.msg424558#msg424558
EntLast()/EntNext() approach http://www.theswamp.org/index.php?topic=37409.msg424652#msg424652
WblockCloneObjects approach http://www.theswamp.org/index.php?topic=37409.msg424709#msg424709

Have fun

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #40 on: March 22, 2011, 07:34:25 AM »
Btw, how do I get all placed entities so I can change the objects layer? Or should I iterate all objects in the TmpDB first?

You could read further upthread:
ObjectAppended event approach http://www.theswamp.org/index.php?topic=37409.msg424558#msg424558
EntLast()/EntNext() approach http://www.theswamp.org/index.php?topic=37409.msg424652#msg424652
WblockCloneObjects approach http://www.theswamp.org/index.php?topic=37409.msg424709#msg424709

Have fun


Thanks! How did I overlook this... :-)

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.

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #41 on: March 27, 2011, 06:35:00 AM »
Uhmm, weird... I have the following code:

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports AcUtils = Autodesk.AutoCAD.Internal.Utils

Public Class clsImportDXF

  <CommandMethod("InsertDxf")> _
  Public Sub InsertDxf()
    Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
    Dim TmpDb As Database = New Database(False, True)
    TmpDb.DxfIn("G:\\test.dxf", IO.Path.GetTempFileName)

    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 New Matrix3d
    Transform = Matrix3d.Scaling(scale, pnt2) * Matrix3d.Displacement(pnt1.GetVectorTo(pnt2))

    db.Insert(Transform, TmpDb, True)


' Change object color -----------------------------
    Dim lastEnt As ObjectId = AcUtils.EntLast()
    Dim idColl As New ObjectIdCollection()

    Using tr As Transaction = doc.TransactionManager.StartTransaction()
      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 = TryCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
        acEntity.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)
      Next
      tr.Commit()
    End Using

  End Sub

After inserting the dxf I try to change the color of the objects. While debugging it loops through all the inserted elements but it does not change the color. I get this error while debugging:

"Cannot evaluate expression because we are stopped in a place where garbage collection is impossible, possibly because the code of the current method may be optimized."

 :?
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.

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #42 on: March 27, 2011, 09:39:41 AM »
Code: [Select]
    db.Insert(Transform, TmpDb, True)


' Change object color -----------------------------
    Dim lastEnt As ObjectId = AcUtils.EntLast()

After inserting the dxf I try to change the color of the objects. While debugging it loops through all the inserted elements but it does not change the color. I get this error while debugging:
Can't help you there, but aren't you supposed to to get the last entity before the insert operation? That is, switch the order of the two statements quoted.

Another optimization may be using the ColorIndex method...

huiz

  • Swamp Rat
  • Posts: 853
  • Certified Prof C3D
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #43 on: March 27, 2011, 02:15:42 PM »
..
Can't help you there, but aren't you supposed to to get the last entity before the insert operation? That is, switch the order of the two statements quoted.

Another optimization may be using the ColorIndex method...

I assume the last entity is what I'v just inserted. While debugging it loops through all the inserted objects but I can't change color or layer or any other property. So getting the inserted items as last entity is correct, but why can't I access them?
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.

Jeff H

  • Needs a day job
  • Posts: 6055
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #44 on: March 27, 2011, 03:47:50 PM »
Hey Huiz, I tested your code real quik and here is what I noticed

Using a new drawing(empty drawing)

This would get the last entity added from 'DxfInsert' called
Code: [Select]
            ' Change object color -----------------------------
            Dim lastEnt As ObjectId = AcUtils.EntLast()


Then this would make it null
Code: [Select]
Dim nextent As ObjectId = AcUtils.EntNext(lastEnt)
so this is never called
Code: [Select]
                    Dim acEntity As Entity = TryCast(tr.GetObject(id, OpenMode.ForWrite), Entity)
                    acEntity.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1)

To get it to work

Using a new drawing(empty drawing) and removing the if statement
Code: [Select]
                If lastEnt = ObjectId.Null Then
                    lastEnt = AcUtils.EntFirst()
                    idColl.Add(lastEnt)
                End If
changed to
Code: [Select]
                lastEnt = AcUtils.EntFirst()
                idColl.Add(lastEnt)

Now lastEnt  is the ObjectId of the first entity added from calling 'DxfInset' of course with a empty drawing