TheSwamp

Code Red => .NET => Topic started by: dbroada on September 21, 2010, 05:46:52 AM

Title: migrating VBA Import
Post by: dbroada 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)


Title: Re: migrating VBA Import
Post by: Kerry on September 21, 2010, 07:52:27 AM

object Import(string FileName, object InsertionPoint, double ScaleFactor);
 
Declaring Type: Autodesk.AutoCAD.Interop.IAcadDocument
Title: Re: migrating VBA Import
Post by: dbroada 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
Title: Re: migrating VBA Import
Post by: dbroada 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.  :-(
Title: Re: migrating VBA Import
Post by: Bryco 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
Title: Re: migrating VBA Import
Post by: dbroada 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)
Title: Re: migrating VBA Import
Post by: Jeff H 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

Title: Re: migrating VBA Import
Post by: dbroada 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"> :?
Title: Re: migrating VBA Import
Post by: Kerry 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);
        }
    }

}
Title: Re: migrating VBA Import
Post by: dbroada 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....
Title: Re: migrating VBA Import
Post by: Kerry 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.
Title: Re: migrating VBA Import
Post by: dbroada 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.  :-)
Title: Re: migrating VBA Import
Post by: Kerry 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
Title: Re: migrating VBA Import
Post by: Kerry 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 :)
Title: Re: migrating VBA Import
Post by: Glenn R on September 22, 2010, 08:03:16 AM
That reminds me of the saying 'I may be cheap, but I'm rough'
Title: Re: migrating VBA Import
Post by: dbroada on September 22, 2010, 09:34:25 AM
right, clearly out of my depth here (but thanks for the help so far). At least I know what I want can be done, its just that I can do it.  :-(

here is the code I am (attempting to) running. If I uncomment the Dim acDoc As AcadDocument = doc.AcadDocument line I get an error message (hopefully produced below).

To my understanding VB 2010 express doesn't support debugging but by use of MsgBox I know that is the line it is failing on. Before I give up for winter  :cry: is there anything obvious that I haven't yet done?

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

Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.IO

Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application


Public Class Hellooo

    <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

Title: Re: migrating VBA Import
Post by: Kerry on September 22, 2010, 09:55:28 AM
How about this
Code: [Select]
' CodeHimBelonga kdub@theSwamp 20100922
'
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
'
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System.IO
Imports System.Windows.Forms

Namespace ducks_vb_

    Public Class MyCommands

        <CommandMethod("Ducks", (CommandFlags.Modal + CommandFlags.Session))> _
        Public Sub DucksIn()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim pr As PromptResult = ed.GetString(("" & vbCrLf + "Enter qualified File: "))
            If Not (pr.Status = PromptStatus.OK) Then
                Return
            End If

            Dim myFile As String = pr.StringResult
            If Not File.Exists(myFile) Then
                Dim msg As String = String.Format("File {0} does not exist", myFile)
                MessageBox.Show(msg, "Ooooooops")
                Return
            End If

            Dim comDoc As AcadDocument = CType(doc.AcadDocument, Autodesk.AutoCAD.Interop.AcadDocument)
            Dim myScale As Double = 1
            Dim p1 As Point3d = Point3d.Origin
            Dim ptArray() As Double = p1.ToArray
            comDoc.Import(myFile, CType(ptArray, Object), myScale)

        End Sub
    End Class

End Namespace

I'll be back shortly .. need to go scrub my hands  :lol:
Title: Re: migrating VBA Import
Post by: dbroada on September 22, 2010, 10:01:33 AM
I thank you now for getting your hands dirty and hopefully I will be back tomorrow (really MUST do some project work today) thanking you for an excellent routine.
Title: Re: migrating VBA Import
Post by: Kerry on September 22, 2010, 10:13:19 AM
Washed my mouth out too ... vb does that to me  :|

You're welcome ... I learnt stuff too.


Note that I had the CommandFlags OR'd ; I've changed them to And'd
ie
& in C#   [added] WRONG .. should be | or + ; kdub 20100923
+ in VB    <- [added] Can someone confirm this Please ?
Title: Re: migrating VBA Import
Post by: mohnston on September 22, 2010, 12:44:11 PM
Washed my mouth out too ... vb does that to me  :|

You're welcome ... I learnt stuff too.


Note that I had the CommandFlags OR'd ; I've changed them to And'd
ie
& in C#
+ in VB    <- [added] Can someone confirm this Please ?

There is both an & operator and an && operator.
I copied the help file below.

The & operator can function as either a unary or a binary operator.

Remarks
The unary & operator returns the address of its operand (requires unsafe context).

Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

The & operator evaluates both operators regardless of the first one's value.

&&
The conditional-AND operator ( &&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.



oh . . . . you meant the vb didn't you? he he
Title: Re: migrating VBA Import
Post by: Kerry on September 22, 2010, 06:29:43 PM

OK, I've checked by testing.
The AutoCAD .NET Developer's Guide Command Definitions page is wrong.  :-o

The example shows the CommandFlags using & in the sample
It SHOULD use | or +


The Values are :
Quote
    [Flags]
    public enum CommandFlags
    {
        TempShowDynDimension = -2147483648,
        Modal = 0,
        Transparent = 1,
        UsePickSet = 2,
        Redraw = 4,
        NoPerspective = 8,
        NoMultiple = 16,
        NoTileMode = 32,
        NoPaperSpace = 64,
        NoOem = 256,
        Undefined = 512,
        InProgress = 1024,
        Defun = 2048,
        NoNewStack = 65536,
        NoInternalLock = 131072,
        DocReadLock = 524288,
        DocExclusiveLock = 1048576,
        Session = 2097152,
        Interruptible = 4194304,
        NoHistory = 8388608,
        NoUndoMarker = 16777216,
        NoBlockEditor = 33554432,
        NoActionRecording = 67108864,
        ActionMacro = 134217728,
        NoInferConstraint = 1073741824,
    }

This Piccy Tells the story I think.
I'll advise Kean and see if we can get it fixed.
Title: Re: migrating VBA Import
Post by: jgr on September 22, 2010, 07:42:56 PM
With vb you can use the logical operator. This is correct :

Code: [Select]
<CommandMethod("Ducks", (CommandFlags.Modal Or CommandFlags.Session))> _
Public Sub DucksIn()

Title: Re: migrating VBA Import
Post by: dbroada on September 23, 2010, 05:17:48 AM
now you are in areas that I have no understanding of I'll just report that I finally have something working. I have got myself so confused with all the error messages I have received! However, now that I have built to Net3.5 (rather than 4) and made the Interop.dll copy local I am seeing results with a very minimal version of Kerry's solution. I will now try with his full solution!!!

Many thanks for your time.

I will be back so lets hope somebody asks a question I can answer before I have built up too much debt.
Title: Re: migrating VBA Import
Post by: dbroada on September 23, 2010, 05:30:23 AM
I didn't think I would be back so soon. Kerry, thank you, your code (almost) works perfectly. The only bit not quite right is around the "oooops" message box but that is not a show stopper. And may well be irrelevant after the next question....

What changes would need to be made to run that routine with a supplied file name? I build up loop diagrams from 3 DXF files and combine them into 1 file. I currently use a script to do this which looks something like

Code: [Select]
sdi
1
new
acadiso
vbastmt
DxfImport("P:\Design_Office\E281\LOOPS\Output\July-2010\E281-10-LD001-528_0.dxf")
vbastmt
DxfImport("P:\Design_Office\E281\LOOPS\Output\July-2010\E281-10-LD001-528_1.dxf")
vbastmt
DxfImport("P:\Design_Office\E281\LOOPS\Output\July-2010\E281-10-LD001-528_2.dxf")
saveas
2007
"P:\Design_Office\E281\LOOPS\Output\July-2010\E281-10-LD001-528"
resume

ultimately I want to replace the DxfImport bit with the new .Net routine.
Title: Re: migrating VBA Import
Post by: Glenn R on September 23, 2010, 09:14:02 AM
Why you would ever want to combine these 2 flags is beyond me:

Code: [Select]
CommandFlags.Modal + CommandFlags.Session
Title: Re: migrating VBA Import
Post by: Jeff H on September 23, 2010, 02:34:50 PM
Sorry for the CommandFlags.Modal + CommandFlags.Session
I should of not put it in there thanks for catching that

For the "short circuiting" I thought
C# &&
VB ANDALSO

C# ||
VB ORALSO