TheSwamp
Code Red => .NET => Topic started 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
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
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)
-
object Import(string FileName, object InsertionPoint, double ScaleFactor);
Declaring Type: Autodesk.AutoCAD.Interop.IAcadDocument
-
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?
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
-
making the Import line into Autodesk.AutoCAD.Runtime.Interop has got rid of one error message but not the other. :-(
-
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
-
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)
-
This will get you a little further
This is not ".Nettyish"
<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
-
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"> :?
-
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}
// 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);
}
}
}
-
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....
-
I reference these for development:
[edit:added] the references shown are for MY configuration (Win7;64bit,ACADM2011;VS2010Pro), yours may differ.
-
YEAH!!!!!!
all the underlining has disappered. Now to work out if I understand the solutions offered.
I'm sure I will be back. :-)
-
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
-
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 :)
-
That reminds me of the saying 'I may be cheap, but I'm rough'
-
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?
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
-
How about this
' 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:
-
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.
-
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 ?
-
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
-
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 :
[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.
-
With vb you can use the logical operator. This is correct :
<CommandMethod("Ducks", (CommandFlags.Modal Or CommandFlags.Session))> _
Public Sub DucksIn()
-
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.
-
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
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.
-
Why you would ever want to combine these 2 flags is beyond me:
CommandFlags.Modal + CommandFlags.Session
-
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