.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

New AutoCAD Managed C# Application Wizard

5 REPLIES 5
Reply
Message 1 of 6
csharpbird
652 Views, 5 Replies

New AutoCAD Managed C# Application Wizard

The original “AutoCAD Managed C# Application Wizard” is very simple. Only the “Autodesk.AutoCAD.Runtime” namespace is imported, but the ObjectARX application often needs other namespaces such as “Autodesk.AutoCAD.DatabaseSevices” and “Autodesk.AutoCAD.ApplicationSevices”. Futhermore, the version of “System.dll” assembly and "System.Data.dll” assemly is 1.0 ,but in Visual Studio.net 2003 we need the 1.1 versions.
So I decide to rewrite the wizard to solve the problems.
For more details,you can read this article:
http://www.codeproject.com/useritems/newcswizard.asp
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: csharpbird

csharpbird wrote:
> The original “AutoCAD Managed C# Application Wizard” is very simple. Only the “Autodesk.AutoCAD.Runtime” namespace is imported, but the ObjectARX application often needs other namespaces such as “Autodesk.AutoCAD.DatabaseSevices” and “Autodesk.AutoCAD.ApplicationSevices”. Futhermore, the version of “System.dll” assembly and "System.Data.dll” assemly is 1.0 ,but in Visual Studio.net 2003 we need the 1.1 versions.
> So I decide to rewrite the wizard to solve the problems.
> For more details,you can read this article:
> http://www.codeproject.com/useritems/newcswizard.asp

Thank you.


--
David Kurtz
Peckham & Wright Architects, Inc.
Columbia, Missouri
Message 3 of 6
banguero
in reply to: csharpbird

I am trying to create a C# application that loops through every element in the model space of an AutoCAD application.
I have the following code in VB that does the job, but I need it in C#

VB code:
Dim mspace As Object
Dim elem As Object
acadApp.Documents.Open("C:\test.dwg")
mspace = acadApp.ActiveDocument.ModelSpace()
For Each elem In mspace
...
Next
But when I translate that to C# i get an error
foreach ( object elem in mspace ) {
...
}
saying: " foreach statement cannot operate on variables of type 'object' because 'object' does not contain a definition for 'GetEnumerator', or it is inaccessible"
Do you have any C# example to loop through the model space successfully?
Thanks in Advance!!
Edgar
banguero@yahoo.com
Message 4 of 6
Anonymous
in reply to: csharpbird

The problem is you shouldn't be using that code in VB.NET either! The
bigger problem, though, is the IDE let you get away with it. Assuming you
are doing this through the COM approach, your code should be something
like:

Dim elem As AcadObject
acadApp.Documents.Open("C:\test.dwg")
Dim ieObjList as IEnumerator = _
acadApp.ActiveDocument.ModelSpace.GetEnumerator
While ieObjList.MoveNext
elem = ieObjList.Current
......
End While

Now even this isn't the best approach but its more accurate than yours.
Ideally, I'd go straight managed code and use a transaction that access the
modelspace.GetAllObjects. This retrieves a DBObjectCollection that you can
iterate through.

-- Mike
___________________________
Mike Tuersley
___________________________
the trick is to realize that there is no spoon...
Message 5 of 6
Anonymous
in reply to: csharpbird

Hi Mike. Can't seem to make any sense
of what you say below, both the example
code you show, and the reference to the
managed "modelspace.GetAllObjects()"
method.

What modelspace.GetAllObjects() method
are you talking about?

Also, would you mind telling us who told
you that using IEnumerator is the 'proper'
way to iterate over ActiveX collections
(in any .NET language) ?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Mike Tuersley" wrote in message news:4944844@discussion.autodesk.com...
The problem is you shouldn't be using that code in VB.NET either! The
bigger problem, though, is the IDE let you get away with it. Assuming you
are doing this through the COM approach, your code should be something
like:

Dim elem As AcadObject
acadApp.Documents.Open("C:\test.dwg")
Dim ieObjList as IEnumerator = _
acadApp.ActiveDocument.ModelSpace.GetEnumerator
While ieObjList.MoveNext
elem = ieObjList.Current
......
End While

Now even this isn't the best approach but its more accurate than yours.
Ideally, I'd go straight managed code and use a transaction that access the
modelspace.GetAllObjects. This retrieves a DBObjectCollection that you can
iterate through.

-- Mike
___________________________
Mike Tuersley
___________________________
the trick is to realize that there is no spoon...
Message 6 of 6
Anonymous
in reply to: csharpbird

I'm not sure what Mike was thinking when he wrote
that reply, but the basic problem your code has is
that it declares variables as 'object', which you can
do in VB, but not in C#.

Contrary to what Mike shows, you do not use the
IEnumerator to iterate ActiveX collections, since
the foreach construct knows how to do that for you.

Here's a simple example that iterates over each
entity in the modelspace collection, in two ways.
The important thing to note is that variables are
not declared as 'Object' type.

////////////////////////////////////////////////////////
using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Interop.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace ActiveXIterator
{
public class Class1
{
public Class1()
{
}

[CommandMethod("SAMPLE")]
public void Sample()
{
AcadApplication app = (AcadApplication) AcadApp.AcadApplication;
AcadModelSpace mspace = app.ActiveDocument.ModelSpace;

// One way to iterate the modelspce collection:

int i = 0;
foreach( AcadEntity entity in mspace )
{
Prompt("\nElement {0} EntityName = {1}", i++, entity.EntityName);
}

// Another way to iterate the modelspace collection:

int cnt = mspace.Count;
for( int k = 0; k < cnt; k++ )
{
AcadEntity e = mspace.Item(k);
Prompt("\nitem[{0}].EntityName = {1}", k, e.EntityName);
}
}

public void Prompt(string fmt, params object[] args)
{
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(fmt, args);
}

}
}



--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

wrote in message news:4944740@discussion.autodesk.com...
I am trying to create a C# application that loops through every element in the model space of an AutoCAD application.
I have the following code in VB that does the job, but I need it in C#

VB code:
Dim mspace As Object
Dim elem As Object
acadApp.Documents.Open("C:\test.dwg")
mspace = acadApp.ActiveDocument.ModelSpace()
For Each elem In mspace
...
Next
But when I translate that to C# i get an error
foreach ( object elem in mspace ) {
...
}
saying: " foreach statement cannot operate on variables of type 'object' because 'object' does not contain a definition for 'GetEnumerator', or it is inaccessible"
Do you have any C# example to loop through the model space successfully?
Thanks in Advance!!
Edgar
banguero@yahoo.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost