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