.NET

Reply
Active Contributor
Posts: 34
Registered: ‎08-22-2005
Message 1 of 6 (200 Views)

New AutoCAD Managed C# Application Wizard

200 Views, 5 Replies
08-22-2005 07:42 PM
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
*David Kurtz
Message 2 of 6 (200 Views)

Re: New AutoCAD Managed C# Application Wizard

08-26-2005 01:39 PM 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
Member
Posts: 4
Registered: ‎08-31-2005
Message 3 of 6 (200 Views)

Re: New AutoCAD Managed C# Application Wizard

08-31-2005 03:53 PM 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
*Mike Tuersley
Message 4 of 6 (200 Views)

Re: New AutoCAD Managed C# Application Wizard

08-31-2005 08:48 PM 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...
*Tony Tanzillo
Message 5 of 6 (200 Views)

Re: New AutoCAD Managed C# Application Wizard

09-02-2005 12:31 AM 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...
*Tony Tanzillo
Message 6 of 6 (200 Views)

Re: New AutoCAD Managed C# Application Wizard

09-02-2005 10:30 AM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community