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

0 Members and 1 Guest are viewing this topic.

MGorecki

  • Guest
Using VB.Net to Import/Insert a DXF File in Existing Drawing
« on: March 09, 2011, 10:17:51 AM »
Hello,
I've looked all over for information on how to insert a DXF file into an existing drawing, but cannot find anything that works.
Does someone have a way to insert a DXF file into an existing drawing using VB.Net?  I've updated my references to include the AuoDesk.AutoCAD.Interop and Interop.Common, but still cannot work out a way to insert a DXF file.

Thanks in advance for any help or at least pointing me in the right direction.
Mark

Jeff H

  • Needs a day job
  • Posts: 6075
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #1 on: March 09, 2011, 01:21:25 PM »
Have you looked at Database.DxfIn()

Jeff H

  • Needs a day job
  • Posts: 6075
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #2 on: March 09, 2011, 01:37:05 PM »
Sorry, Mixed up forget the DxfIN
 seems I have done it with Regular Insert

Jeff H

  • Needs a day job
  • Posts: 6075
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #3 on: March 09, 2011, 01:40:13 PM »
I remember now, post at Autodesk fourms

This code is from Kerry Brown at http://www.theswamp.org/index.php?topic=34982.0 with a litle a change so thank him.

 
[CommandMethod("ImportDXF")]
          public void ImportDXF()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;       
            AcadDocument comDoc = (Autodesk.AutoCAD.Interop.AcadDocument)doc.AcadDocument;
            double myScale = 1;
            Point3d p1 = Point3d.Origin;
            double[] ptArray = p1.ToArray();
            comDoc.Import(@"C:\Users\Jeff\My Documents\XrefTest.dxf", (object)ptArray, myScale);
        }

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #4 on: March 10, 2011, 11:00:58 AM »
This code is from Kerry Brown at http://www.theswamp.org/index.php?topic=34982.0 with a litle a change so thank him.

Is it just me or can't the insertion point be set? (Scale factor works fine btw.)

Code: [Select]
        let ppr = ed.GetPoint "Insertion point"
        if ppr.Status = PromptStatus.OK then
            let pdr = ed.GetDouble "Scale factor"
            if pdr.Status = PromptStatus.OK then
                let objs = new ResizeArray<_>()
                (
                    use objectAppended =
                        db.ObjectAppended
                        |> Observable.subscribe
                            (fun e -> objs.Add e.DBObject.ObjectId)
                    doc.AcadDocument?Import(dialog.FileName, ppr.Value.ToArray(), pdr.Value)
                )
                objs |> Array.ofSeq |> ed.SetImpliedSelection


Jeff H

  • Needs a day job
  • Posts: 6075
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #5 on: March 10, 2011, 05:13:38 PM »
Is it just me or can't the insertion point be set? (Scale factor works fine btw.)

Was keeping it simple

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #6 on: March 11, 2011, 04:26:09 AM »
Was keeping it simple

Kerry (thx) kept it simple too. Me wants complicated.

What exactly needs to be passed to the Import method to influence the insertion point? (System.Double[] likely doesn't cut it)

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 #7 on: March 11, 2011, 09:01:54 AM »
kaefer ,

I'm unsure of the f# syntax, and how much goes on behind the scenes regarding typing, but

I think you will need to cast the array of doubles to an object.
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<--

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #8 on: March 11, 2011, 04:45:20 PM »
I'm unsure of the f# syntax, and how much goes on behind the scenes regarding typing, but
Oh, sorry. I think its the same as in C#, autoboxing when assigning to object[].

Quote
I think you will need to cast the array of doubles to an object.

My problem doesn't seem to be the missing cast, but an unintuative interpretation of "insertion point" by the Import method. The following C# code exhibits the same weird behaviour: treating the insertion point as 0,0,0 when the scale factor is 1.0, else do something, but not what I'd expect.

Code: [Select]
   public class MyDxfInCmdClass
    {
        IList<ObjectId> objs;

        [CommandMethod("MyDxfIn")]
        public void MyDxfInCmd()
        {
            objs = new List<ObjectId>();
            Document doc = AcApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            var dialog = new System.Windows.Forms.OpenFileDialog{
                    CheckFileExists = true,
                    CheckPathExists = true,
                    DefaultExt = "dxf",
                    DereferenceLinks = true,
                    Filter = "DXF Files (*.dxf)|*.dxf|All files (*.*)|*.*" ,
                    Title = "Select dxf file" };
            if(dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            var ppr = ed.GetPoint("Insertion point");
            if(ppr.Status != PromptStatus.OK)
                return;
            var pdr = ed.GetDouble("Scale factor");
            if(pdr.Status != PromptStatus.OK)
                return;
            db.ObjectAppended += new ObjectEventHandler(ObjectAppended);
            try
            {
                object[] args = new object[] {
                    dialog.FileName, ppr.Value.ToArray(), pdr.Value
                };
                Invoke(doc.AcadDocument, "Import", args);
            }
       finally
       {
       db.ObjectAppended -= new ObjectEventHandler(ObjectAppended);
       }
            ObjectId[] ids = new ObjectId[objs.Count];
            objs.CopyTo(ids, 0);
            ed.SetImpliedSelection(ids);
        }
        void ObjectAppended(object sender, ObjectEventArgs e)
        {
            objs.Add(e.DBObject.ObjectId);
        }
        static object Invoke(object o, string name, params object[] args)
        {
            return o.GetType().InvokeMember(name, System.Reflection.BindingFlags.InvokeMethod, null, o, args);
        }
    }
« Last Edit: March 11, 2011, 04:48:40 PM by kaefer »

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 #9 on: March 11, 2011, 09:58:11 PM »
The attached piccy shows my testing routine.

There seems to be a bug in the  Autodesk.AutoCAD.Interop.Import()
Code: [Select]
comDoc.Import(fn, (object)ptArray, insScale);If I insert at 100,200,0 with a scale of 1 the dxf comes in at 0,0,0 at 1:1

If I insert at 100,200,0 with a scale of 2 the dxf comes in at -100,-200,0 at 2:1

If I insert at 100,200,0 with a scale of 3 the dxf comes in at -200,-400,0 at 3:1

I'm sure you see the progression :)

I'll do some more testing and report it to the ADN.
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<--

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 #10 on: March 11, 2011, 10:23:44 PM »
One workaround may be :

Identify the last entity in the DataBase.

Collect the FileName, Location, Scale.

Import at 0,0,0 @ scale.

Build a collection of entitys in the Database after the previously identified last entity.

Move the collection from 0,0,0 to Location(factored).
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<--

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 #11 on: March 12, 2011, 03:23:00 AM »
Thinking about this while I was out ...
Another workaround may be :

Identify and save UCS information ; either Name or Coordinates.

Collect the FileName, Location, Scale.

Change the UCS Origin

Import at 0,0,0 @ scale.

Restore the Previous UCS

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<--

kaefer

  • Swamp Rat
  • Posts: 572
Re: Using VB.Net to Import/Insert a DXF File in Existing Drawing
« Reply #12 on: March 12, 2011, 04:57:34 AM »
Lots of "thank you!" for confirming my suspicions.

Change the UCS Origin

Won't do, because Import lives in world coordinates.

Collect the FileName, Location, Scale.

Import at 0,0,0 @ scale.

Build a collection of entitys in the Database after the previously identified last entity.

Move the collection from 0,0,0 to Location(factored).

This is the way to go, then. The ObjectAppended event may be helpful to collect the entities and a transformation matrix build of displacement and rotation (extra bonus) does the work.

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 #13 on: March 12, 2011, 05:38:25 AM »

I've reported the anomolie.

I'll have a play tomorrow if I can make time ... unless 'someone' posts a solution in the meantime :)

I have code in Lisp for doing what is needed so I know the idea works.
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 #14 on: March 12, 2011, 05:23:50 PM »
Has anybody tried:

1. New 'side database'
2. DxfIn method on new 'side database' to insert dxf file into new pristine database
3. insert the new 'side database' with dxf info into current drawing

???
Me