Author Topic: migrating VBA Import  (Read 10600 times)

0 Members and 1 Guest are viewing this topic.

dbroada

  • Guest
Re: migrating VBA Import
« Reply #15 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


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #16 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:
« Last Edit: September 22, 2010, 10:09:10 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 #17 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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #18 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 ?
« Last Edit: September 22, 2010, 06:31:14 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.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: migrating VBA Import
« Reply #19 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
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: migrating VBA Import
« Reply #20 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.
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.

jgr

  • Guest
Re: migrating VBA Import
« Reply #21 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()


dbroada

  • Guest
Re: migrating VBA Import
« Reply #22 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.

dbroada

  • Guest
Re: migrating VBA Import
« Reply #23 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.

Glenn R

  • Guest
Re: migrating VBA Import
« Reply #24 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

Jeff H

  • Needs a day job
  • Posts: 6150
Re: migrating VBA Import
« Reply #25 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