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

is DBObjectCollection bug ?

10 REPLIES 10
Reply
Message 1 of 11
sieben
567 Views, 10 Replies

is DBObjectCollection bug ?

DBObjectCollection objColl = curve.GetSplitCurves(dColl);
//OK two entitys will be drawed ::type 1
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[1];
ent = (Entity)objColl[2];
DrawEntity(ent);
}
//OK three entitys will be drawed ::type 2
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[1];
DrawEntity(ent);
ent = (Entity)objColl[2];
DrawEntity(ent);
}
//Error: can not access objColl[2] ::type 3
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[2];
DrawEntity(ent);
}
is DBObjectCollection bug or other reason ?
10 REPLIES 10
Message 2 of 11
Anonymous
in reply to: sieben

>> Error: can not access objColl[2] ::type 3

Is that the error? If not, what is the error?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5607043@discussion.autodesk.com...
DBObjectCollection objColl = curve.GetSplitCurves(dColl);
//OK two entitys will be drawed ::type 1
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[1];
ent = (Entity)objColl[2];
DrawEntity(ent);
}
//OK three entitys will be drawed ::type 2
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[1];
DrawEntity(ent);
ent = (Entity)objColl[2];
DrawEntity(ent);
}
//Error: can not access objColl[2] ::type 3
if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
DrawEntity(ent);
ent = (Entity)objColl[2];
DrawEntity(ent);
}
is DBObjectCollection bug or other reason ?
Message 3 of 11
sieben
in reply to: sieben

// thanks Tony Tanzillo !!!
// the total caode as follow

[CommandMethod("SSG")]
public void GeometryTrans()
{
//**************begin here
Line line1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
adb.DrawLine(line1); //Transaction commit entity to WorkingDatabase
using (Transaction ctrans = mmc.db.TransactionManager.StartTransaction())
{
Line line2 = (Line)ctrans.GetObject(line1.ObjectId, OpenMode.ForWrite);
//***************
BreakCurve(line2, new Point3d(20, 0, 0), new Point3d(50, 0, 0));
//****************
ctrans.Commit();
}
}
public bool BreakCurve(Curve curve, Point3d p1, Point3d p2)
{
try
{
Point3d p11 = curve.GetClosestPointTo(p1, false);
double param1 = curve.GetParameterAtPoint(p11);
Point3d p21 = curve.GetClosestPointTo(p2, false);
double param2 = curve.GetParameterAtPoint(p21);
DoubleCollection dColl = new DoubleCollection(2);
if (param1 < param2)
{
dColl.Add(param1);
dColl.Add(param2);
}
else
{
dColl.Add(param2);
dColl.Add(param1);
}
DBObjectCollection objColl = curve.GetSplitCurves(dColl);
Entity ent;
if (objColl.Count == 2)
{
ent = (Entity)objColl[1];
adb.DrawEntity(ent);
}
else if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
adb.DrawEntity(ent);
//*****************************
//ent = (Entity)objColl[1]; // if not note this line code ,all will be OK,
//adb.DrawEntity(ent);

ent = (Entity)objColl[2]; //here catch System.Exception ex
//ex.Message = Insertion index was out of range. Must be non-negative and
//less than or equal to size.Parameter name: index
//***********************
adb.DrawEntity(ent);
}
curve.Erase();
return true;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
catch (System.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
}
Message 4 of 11
Anonymous
in reply to: sieben

That does appear to be a bug.

Try doing this just before using the default indexer:

foreach(DBObject obj in objColl)
{
if( obj == null )
throw new InvalidOperationException();
}

The above will force the creation of wrappers for
the entire collection (DBObjectCollection doesn't
create wrappers for native objects until they are
actually requested).

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5608096@discussion.autodesk.com...
// thanks Tony Tanzillo !!!
// the total caode as follow

[CommandMethod("SSG")]
public void GeometryTrans()
{
//**************begin here
Line line1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
adb.DrawLine(line1); //Transaction commit entity to WorkingDatabase
using (Transaction ctrans = mmc.db.TransactionManager.StartTransaction())
{
Line line2 = (Line)ctrans.GetObject(line1.ObjectId, OpenMode.ForWrite);
//***************
BreakCurve(line2, new Point3d(20, 0, 0), new Point3d(50, 0, 0));
//****************
ctrans.Commit();
}
}
public bool BreakCurve(Curve curve, Point3d p1, Point3d p2)
{
try
{
Point3d p11 = curve.GetClosestPointTo(p1, false);
double param1 = curve.GetParameterAtPoint(p11);
Point3d p21 = curve.GetClosestPointTo(p2, false);
double param2 = curve.GetParameterAtPoint(p21);
DoubleCollection dColl = new DoubleCollection(2);
if (param1 < param2)
{
dColl.Add(param1);
dColl.Add(param2);
}
else
{
dColl.Add(param2);
dColl.Add(param1);
}
DBObjectCollection objColl = curve.GetSplitCurves(dColl);
Entity ent;
if (objColl.Count == 2)
{
ent = (Entity)objColl[1];
adb.DrawEntity(ent);
}
else if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
adb.DrawEntity(ent);
//*****************************
//ent = (Entity)objColl[1]; // if not note this line code ,all will be OK,
//adb.DrawEntity(ent);

ent = (Entity)objColl[2]; //here catch System.Exception ex
//ex.Message = Insertion index was out of range. Must be non-negative and
//less than or equal to size.Parameter name: index
//***********************
adb.DrawEntity(ent);
}
curve.Erase();
return true;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
catch (System.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
}
Message 5 of 11
Anonymous
in reply to: sieben

That does look like a bug.

Try doing this before using the default indexer:

foreach(DBObject obj in objColl)
{
if( obj == null )
throw new InvalidOperationException();
}

The above will force the creation of wrappers for
the entire collection (DBObjectCollection doesn't
create wrappers for native objects until they are
actually requested).

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5608096@discussion.autodesk.com...
// thanks Tony Tanzillo !!!
// the total caode as follow

[CommandMethod("SSG")]
public void GeometryTrans()
{
//**************begin here
Line line1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 0, 0));
adb.DrawLine(line1); //Transaction commit entity to WorkingDatabase
using (Transaction ctrans = mmc.db.TransactionManager.StartTransaction())
{
Line line2 = (Line)ctrans.GetObject(line1.ObjectId, OpenMode.ForWrite);
//***************
BreakCurve(line2, new Point3d(20, 0, 0), new Point3d(50, 0, 0));
//****************
ctrans.Commit();
}
}
public bool BreakCurve(Curve curve, Point3d p1, Point3d p2)
{
try
{
Point3d p11 = curve.GetClosestPointTo(p1, false);
double param1 = curve.GetParameterAtPoint(p11);
Point3d p21 = curve.GetClosestPointTo(p2, false);
double param2 = curve.GetParameterAtPoint(p21);
DoubleCollection dColl = new DoubleCollection(2);
if (param1 < param2)
{
dColl.Add(param1);
dColl.Add(param2);
}
else
{
dColl.Add(param2);
dColl.Add(param1);
}
DBObjectCollection objColl = curve.GetSplitCurves(dColl);
Entity ent;
if (objColl.Count == 2)
{
ent = (Entity)objColl[1];
adb.DrawEntity(ent);
}
else if (objColl.Count == 3)
{
ent = (Entity)objColl[0];
adb.DrawEntity(ent);
//*****************************
//ent = (Entity)objColl[1]; // if not note this line code ,all will be OK,
//adb.DrawEntity(ent);

ent = (Entity)objColl[2]; //here catch System.Exception ex
//ex.Message = Insertion index was out of range. Must be non-negative and
//less than or equal to size.Parameter name: index
//***********************
adb.DrawEntity(ent);
}
curve.Erase();
return true;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
catch (System.Exception ex)
{
smc.WriteLine("BreakCurve Failure : " + ex.Message, 0);
return false;
}
}
Message 6 of 11
sieben
in reply to: sieben

Tony Tanzillo : thank you very much!
you code can prevent the exception.
I think what it do is to visit the DBObjectCollection
form index of 0,must in turn,when visit the index,do something
as initialize the member of DBObjectCollection or complete
DBObjectCollection stack.something like my code ,before visit
index must visit index[i-1].
Message 7 of 11
Anonymous
in reply to: sieben

Hi Sieben
Sorry for off-topic
Can you share the local functions:
GetSplitCurves and DrawEntity here
That's very interesting for me

Thanks

~'J'~
Message 8 of 11
Anonymous
in reply to: sieben

GetSplitCurves() is a method of the Curve class.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5608528@discussion.autodesk.com...
Hi Sieben
Sorry for off-topic
Can you share the local functions:
GetSplitCurves and DrawEntity here
That's very interesting for me

Thanks

~'J'~
Message 9 of 11
Anonymous
in reply to: sieben

Aha! Thanks again

~'J'~
Message 10 of 11
sieben
in reply to: sieben

Hi Fatty! DrawEntity funtion code as follow,
GetSplitCurves() as Tony Tanzillo say.
public static ObjectId DrawEntity(Entity ent)

try
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return ent.ObjectId;
}
catch
{
return ObjectId.Null;
}
}
Message 11 of 11
Anonymous
in reply to: sieben

Thanks a lot
Cheers 🙂

~'J'~

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