Author Topic: migrating VBA Import  (Read 10498 times)

0 Members and 1 Guest are viewing this topic.

dbroada

  • Guest
migrating VBA Import
« on: September 21, 2010, 05:46:52 AM »
Hi,
I am migrating some of my VBA code to .Net and thought I had found an easy routine to practice on. I may be wrong! I bring (upto) 3 dxf files into a single drawing and then save the file. To do this I use the following sub

Code: [Select]
Public Sub DxfImport(myFile As String)
Dim P1(0 To 2) As Double
Dim myScale As Double
Dim myTest

If Right$(myFile, 4) <> ".dxf" Then myFile = myFile & ".dxf"
myTest = Dir$(myFile)
If myTest <> "" Then
    P1(0) = 0: P1(1) = 0: P1(2) = 0
    myScale = 1
   
    ThisDrawing.Import myFile, P1, myScale
   
    End If

End Sub

What I can't find is a .Net equivalent of the
Code: [Select]
ThisDrawing.Import myFile, P1, myScale line. Is there such a thing and if there is, how do I access it?
(this is the VBA Import, not the AutoCAD import)



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #1 on: September 21, 2010, 07:52:27 AM »

object Import(string FileName, object InsertionPoint, double ScaleFactor);
 
Declaring Type: Autodesk.AutoCAD.Interop.IAcadDocument
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.

dbroada

  • Guest
Re: migrating VBA Import
« Reply #2 on: September 21, 2010, 08:59:59 AM »
thank you, that looks easy. However I am obviously missing something here (i'm new to .Net - not too obvious I hope).
I'm still not sure how to put that in to practice. I have the following but still have errors at the .interop & (Import stages. Have I failed to reference something somewhere?

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

Public Class Hellooo
    <CommandMethod("Ducks")> _
    Public Sub DucksIn(ByVal myFile As String)

        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim p1(0 To 2) As Double
        Dim myScale As Double
        Dim myTest
        If Right(myFile, 4) <> ".DXF" Then myFile = myFile & ".DXF"
        myTest = Dir(myFile)
        If myTest <> "" Then
            p1(0) = 0 : p1(1) = 0 : p1(2) = 0
            myScale = 1
            acDoc(IMPORT(myFile, p1, myScale))
        Else
            MsgBox("File doesn't exist")
        End If

    End Sub

dbroada

  • Guest
Re: migrating VBA Import
« Reply #3 on: September 21, 2010, 09:05:00 AM »
making the Import line into Autodesk.AutoCAD.Runtime.Interop has got rid of one error message but not the other.  :-(

Bryco

  • Water Moccasin
  • Posts: 1883
Re: migrating VBA Import
« Reply #4 on: September 21, 2010, 10:12:05 AM »
In net a point is usually an actual point (point2d or point3d) not just an array of doubles. You may have to make a point3d here, it isn't clear

dbroada

  • Guest
Re: migrating VBA Import
« Reply #5 on: September 21, 2010, 10:15:05 AM »
thank you, but wouldn't that just throw up an error at the relavent term rather than refusing to acknowledge the existance Import element?

(Off to give it a try at next coffee break)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: migrating VBA Import
« Reply #6 on: September 21, 2010, 11:43:41 AM »
This will get you a little further

This is not ".Nettyish"

Code: [Select]

<CommandMethod("Ducks", CommandFlags.Modal + CommandFlags.Session)> _
    Public Sub DucksIn()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim pr As PromptResult = ed.GetString(vbCrLf & "Enter File: ")
        If pr.Status = PromptStatus.OK Then
            Dim myFile As String = pr.StringResult
            Dim fNameNoExt = Path.GetFileNameWithoutExtension(myFile)
            Dim acDoc As AcadDocument = doc.AcadDocument
            Dim p1 As Point3d = Point3d.Origin
            Dim myScale As Double = 1
            If File.Exists(myFile) Then
                myFile = fNameNoExt
            ElseIf Not File.Exists(fNameNoExt & ".dxf") Then
                MsgBox("File does not exist")
            End If
            myScale = 1
            acDoc.Import(myFile, p1, myScale)
        End If
    End Sub


dbroada

  • Guest
Re: migrating VBA Import
« Reply #7 on: September 22, 2010, 03:51:59 AM »
fro2001, it gets me a little closer, but only a little. My system is not recognising the AcadDocument type. If I change that to Document it the throws up an error with the import bit.

Now that several people have helped I guess I should declare my set up just in case I am trying to do something not possible.
I am using AutoCAD 2008 electrical and Microsoft Visual Basic 2010 express.

I have only been formerly trained in Fortran as a programming language (and have dabbled with BASIC, VB & VBC on a self taught basis) so I am struggling to grasp .Net at the moment.

and what do you mean by ".Nettyish"> :?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #8 on: September 22, 2010, 06:35:54 AM »
This works for me.
I'll leave the translation to someone who likes playing with VB

{ Forum Profile->Look And Layout Prerferences -> Current Theme: Mercury}
Code: [Select]

// CodeHimBelonga kdub@theSwamp 20100922
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
//
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.IO;
using System.Windows.Forms;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Ducks.MyCommands))]

namespace Ducks
{
    public class MyCommands
    {
        [CommandMethod("Ducks", (CommandFlags.Modal | CommandFlags.Session))]
        public void DucksIn()
        {
            Document doc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptResult pr = ed.GetString(("\r\n" + "Enter qualified File: "));
            if (!(pr.Status == PromptStatus.OK))
                return;

            string myFile = pr.StringResult;
            if (!File.Exists(myFile))
            {
                string msg = string.Format("File {0} does not exist", myFile);
                MessageBox.Show( msg, "Ooooooops");
                return;
            }

            AcadDocument comDoc = (Autodesk.AutoCAD.Interop.AcadDocument)doc.AcadDocument;

            double myScale = 1;
            Point3d p1 = Point3d.Origin;
            double[] ptArray = p1.ToArray();
            comDoc.Import(myFile, (object)ptArray, myScale);
        }
    }

}
« Last Edit: September 22, 2010, 06:31:56 PM 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.

dbroada

  • Guest
Re: migrating VBA Import
« Reply #9 on: September 22, 2010, 06:58:14 AM »
can I send you several hundred drawings to process  :-D

I think it must be my references rather than the code  :-o as I still can't get the Interop bit to be accepted....

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #10 on: September 22, 2010, 07:21:16 AM »
I reference these for development:


[edit:added] the references shown are for MY configuration (Win7;64bit,ACADM2011;VS2010Pro), yours may differ.
« Last Edit: September 22, 2010, 07:47:42 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.

dbroada

  • Guest
Re: migrating VBA Import
« Reply #11 on: September 22, 2010, 07:33:28 AM »
YEAH!!!!!!

all the underlining has disappered. Now to work out if I understand the solutions offered.

I'm sure I will be back.  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #12 on: September 22, 2010, 07:39:56 AM »

You can also reference the Interops from the GAC ... something like ;
C:\Windows\assembly\GAC_MSIL\Autodesk.AutoCAD.Interop\18.1.0.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.dll
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #13 on: September 22, 2010, 07:44:37 AM »
can I send you several hundred drawings to process  :-D


Dave,
If you like, but it sounds as if you're almost there.

and besides, I'm not cheap even if I am nasty :)
« Last Edit: September 22, 2010, 07:55:58 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.

Glenn R

  • Guest
Re: migrating VBA Import
« Reply #14 on: September 22, 2010, 08:03:16 AM »
That reminds me of the saying 'I may be cheap, but I'm rough'