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

Problem with BlockTableRecord in code

6 REPLIES 6
Reply
Message 1 of 7
Anonymous
279 Views, 6 Replies

Problem with BlockTableRecord in code

I have some problems with following code.
At this line i always have an error, but i don't know why?
BlockTableRecord btr = (BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

And another question.
I'm drawing some lines in this code. Is it possible to make them selectable afterwords, e.g. after the command i want to select them with p (previous) in another command like move?


[code]#region Using directives

using System;
//using System.Collections.Generic;
//using System.Text;
using System.Threading;
using System.Globalization;

using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

#endregion

namespace RSNNAcadApp.ExtendedCommands
{
public class Embankment
{
private int Luprec = Application.DocumentManager.MdiActiveDocument.Database.Luprec;
private bool m_firstTime2Lines = true; //first invocation of "Embankment2Lines"? (per-document)
private bool m_firstTime1Line = true; //first invocation of "Embankment1Line"? (per-document)

private Curve CurveObj1;
private Curve CurveObj2;
private double Dist;
private double LengthLong;
private double LengthShort;

[CommandMethod("Böschung2Kanten", CommandFlags.Session)]
public void Embankment2Lines()
{
Database db = HostApplicationServices.WorkingDatabase;// Application.DocumentManager.MdiActiveDocument.Database; //oder: entid.Database;
Transaction myT = db.TransactionManager.StartTransaction();
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
try
{

string CurveType;

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entopts1 = new PromptEntityOptions("Oberkante zeigen");
entopts1.Message = "Oberkante zeigen";
PromptEntityResult ent1 = null;
bool RightObject1 = false;

do
{
do
{
ent1 = ed.GetEntity(entopts1);
} while (ent1.Status == PromptStatus.Error);

if (ent1.Status == PromptStatus.Cancel)
return;

if (ent1.Status == PromptStatus.OK)
{
ObjectId entid1 = ent1.ObjectId;
Entity tmpEnt1 = (Entity)myT.GetObject(entid1, OpenMode.ForRead);
CurveObj1 = tmpEnt1 as Curve;
if (CurveObj1 != null)
{
CurveType = CurveObj1.GetType().Name;
if (CurveType == "Polyline" || CurveType == "Spline" ||
CurveType == "Line" || CurveType == "Arc" || CurveType == "Ellipse")
{
RightObject1 = true;

//highlight the entity
ObjectId[] ids = new ObjectId[1];
ids[0] = entid1;

SubentityId index = new SubentityId(SubentityType.Edge, 0);
FullSubentityPath path = new FullSubentityPath(ids, index);

tmpEnt1.Highlight(path, true);

PromptEntityOptions entopts2 = new PromptEntityOptions("Unterkante zeigen");
entopts2.Message = "Unterkante zeigen";
PromptEntityResult ent2 = null;
bool RightObject2 = false;

do
{
do
{
ent2 = ed.GetEntity(entopts2);
} while (ent2.Status == PromptStatus.Error);

if (ent2.Status == PromptStatus.Cancel)
{
tmpEnt1.Unhighlight(path, false);
//myT.Commit();
return;
}

if (ent2.Status == PromptStatus.OK)
{
ObjectId entid2 = ent2.ObjectId;
Entity tmpEnt2 = (Entity)myT.GetObject(entid2, OpenMode.ForRead);
CurveObj2 = tmpEnt2 as Curve;
if (CurveObj2 != null)
{
CurveType = CurveObj2.GetType().Name;
if (CurveType == "Polyline" || CurveType == "Spline" ||
CurveType == "Line" || CurveType == "Arc" || CurveType == "Ellipse")
{
RightObject2 = true;

}
else
{
entopts2.Message = "Falsches Objekt!\nUnterkante zeigen";
}
}
}

} while (RightObject2 != true);

tmpEnt1.Unhighlight(path, false);

}
else
{
entopts1.Message = "Falsches Objekt!\nOberkante zeigen";
}
}

}

} while (RightObject1 != true);




PromptDistanceOptions optDist = new PromptDistanceOptions("Abstand zeigen");

optDist.AllowNegative = false;
optDist.AllowZero = false;
optDist.AllowNone = false;
optDist.UseDashedLine = true;
if (!m_firstTime2Lines || !m_firstTime1Line)
optDist.DefaultValue = Dist;

PromptDoubleResult resDist = ed.GetDistance(optDist);

if (resDist.Status != PromptStatus.OK)
return;


PromptDoubleOptions optLong = new PromptDoubleOptions("Länge der langen Böschungslinien in %");

optLong.AllowNegative = false;
optLong.AllowZero = false;
optLong.AllowNone = false;
if (!m_firstTime2Lines)
optLong.DefaultValue = LengthLong;
else
optLong.DefaultValue = 100;

PromptDoubleResult resLong = ed.GetDouble(optLong);

if (resLong.Status != PromptStatus.OK)
return;


PromptDoubleOptions optShort = new PromptDoubleOptions("Länge der kurzen Böschungslinien in %");

optShort.AllowNegative = false;
optShort.AllowZero = false;
optShort.AllowNone = false;
if (!m_firstTime2Lines)
optShort.DefaultValue = LengthShort;
else
optShort.DefaultValue = 50;

PromptDoubleResult resShort = ed.GetDouble(optShort);

if (resShort.Status == PromptStatus.OK)
{
Dist = resDist.Value;
LengthLong = resLong.Value;
LengthShort = resShort.Value;
}
else
{
return;
}

m_firstTime2Lines = false;



double DistGes = Dist;
double CurveObj1Length = CurveObj1.GetDistanceAtParameter(CurveObj1.EndParam);
DBObjectCollection Boeschung = new DBObjectCollection();
ObjectIdCollection IDColl = new ObjectIdCollection();

ed.WriteMessage("Schritt1");
BlockTable bt = (BlockTable)myT.GetObject(db.BlockTableId, OpenMode.ForRead, false);
ed.WriteMessage("Schritt2");

//HERE IS SOME MISTAKE --- I ALWAYS GET AN ERROR, WHY???
//BlockTableRecord btr = (BlockTableRecord)myT.GetObject(CurveObj1.OwnerId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);


ed.WriteMessage("Schritt3");
bool IsLongLine = true;

do
{

Point3d Obj1Punkt = CurveObj1.GetPointAtDist(DistGes);
Point3d Obj2Punkt;
//double Obj1Param = CurveObj1.GetParameterAtDistance(DistGes);
Vector3d Obj1Deriative1 = CurveObj1.GetFirstDerivative(Obj1Punkt);


Xline XTemp = new Xline();
XTemp.UnitDir = new Vector3d(Obj1Deriative1.Y, Obj1Deriative1.X * -1, Obj1Deriative1.Z);
XTemp.BasePoint = Obj1Punkt;

Point3dCollection SPunkte = new Point3dCollection();
XTemp.IntersectWith(CurveObj2, Intersect.OnBothOperands, SPunkte, 0, 0);

if (SPunkte.Count > 0)
{
double DistTemp = 0;
int z = 0;
foreach (Point3d tmpPt in SPunkte)
{
if (z == 0)
{
DistTemp = Obj1Punkt.DistanceTo(tmpPt);
Obj2Punkt = tmpPt;
z++;
}
else
{
if (Obj1Punkt.DistanceTo(tmpPt) < DistTemp)
{
DistTemp = Obj1Punkt.DistanceTo(tmpPt);
Obj2Punkt = tmpPt;
}
}
}
//XTemp.Cancel();

Line Boeschungslinie = new Line(Obj1Punkt, Obj2Punkt);

double Length;
if (IsLongLine)
Length = LengthLong * 0.01;
else
Length = LengthShort * 0.01;

Matrix3d scaleMatrix = Matrix3d.Scaling(Length, Obj1Punkt);


if (Length != 1)
{
Boeschungslinie.TransformBy(scaleMatrix);
}

IsLongLine = (IsLongLine == true) ? false : true;

btr.AppendEntity(Boeschungslinie);
myT.AddNewlyCreatedDBObject(Boeschungslinie, true);
}

DistGes += Dist;
} while (DistGes < CurveObj1Length);

myT.Commit();
}
catch (System.Exception)
{
myT.Commit();
throw;
}

finally
{
myT.Dispose();
}

}
}
}[/code]

Roland
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: Anonymous

Is there no one who can help me???
Does this code work for you?
You have to select two curves, top and bottom, then the distance from the embankmentlines, then the lenght of the short and the long lines in percent.
Something happens with the btr? I do not understand what?

Roland
Message 3 of 7
Anonymous
in reply to: Anonymous

The problem is that you use CommandFlags.Session. It means that you are
responsible for document locking. In fact the exception says:
eLockViolation. (BTW, when a piece of code throws an exception and you want
to get help in the newsgroup then it is always useful to post what the
exception is.:-))

If you replace
[CommandMethod("Böschung2Kanten", CommandFlags.Session)]
with
[CommandMethod("Böschung2Kanten", CommandFlags.Modal)]
it will work.

To make your objects selectable by previous you must use the
Editor.GetSelection API instead of using Editor.GetEntity in a loop.

Albert

wrote in message news:4864376@discussion.autodesk.com...
I have some problems with following code.
At this line i always have an error, but i don't know why?
BlockTableRecord btr =
(BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

And another question.
I'm drawing some lines in this code. Is it possible to make them selectable
afterwords, e.g. after the command i want to select them with p (previous)
in another command like move?


[code]#region Using directives

using System;
//using System.Collections.Generic;
//using System.Text;
using System.Threading;
using System.Globalization;

using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

#endregion

namespace RSNNAcadApp.ExtendedCommands
{
public class Embankment
{
private int Luprec =
Application.DocumentManager.MdiActiveDocument.Database.Luprec;
private bool m_firstTime2Lines = true; //first invocation of
"Embankment2Lines"? (per-document)
private bool m_firstTime1Line = true; //first invocation of
"Embankment1Line"? (per-document)

private Curve CurveObj1;
private Curve CurveObj2;
private double Dist;
private double LengthLong;
private double LengthShort;

[CommandMethod("Böschung2Kanten", CommandFlags.Session)]
public void Embankment2Lines()
{
Database db = HostApplicationServices.WorkingDatabase;//
Application.DocumentManager.MdiActiveDocument.Database; //oder:
entid.Database;
Transaction myT = db.TransactionManager.StartTransaction();
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
try
{

string CurveType;

Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entopts1 = new
PromptEntityOptions("Oberkante zeigen");
entopts1.Message = "Oberkante zeigen";
PromptEntityResult ent1 = null;
bool RightObject1 = false;

do
{
do
{
ent1 = ed.GetEntity(entopts1);
} while (ent1.Status == PromptStatus.Error);

if (ent1.Status == PromptStatus.Cancel)
return;

if (ent1.Status == PromptStatus.OK)
{
ObjectId entid1 = ent1.ObjectId;
Entity tmpEnt1 = (Entity)myT.GetObject(entid1,
OpenMode.ForRead);
CurveObj1 = tmpEnt1 as Curve;
if (CurveObj1 != null)
{
CurveType = CurveObj1.GetType().Name;
if (CurveType == "Polyline" || CurveType ==
"Spline" ||
CurveType == "Line" || CurveType == "Arc" ||
CurveType == "Ellipse")
{
RightObject1 = true;

//highlight the entity
ObjectId[] ids = new ObjectId[1];
ids[0] = entid1;

SubentityId index = new
SubentityId(SubentityType.Edge, 0);
FullSubentityPath path = new
FullSubentityPath(ids, index);

tmpEnt1.Highlight(path, true);

PromptEntityOptions entopts2 = new
PromptEntityOptions("Unterkante zeigen");
entopts2.Message = "Unterkante zeigen";
PromptEntityResult ent2 = null;
bool RightObject2 = false;

do
{
do
{
ent2 = ed.GetEntity(entopts2);
} while (ent2.Status ==
PromptStatus.Error);

if (ent2.Status == PromptStatus.Cancel)
{
tmpEnt1.Unhighlight(path, false);
//myT.Commit();
return;
}

if (ent2.Status == PromptStatus.OK)
{
ObjectId entid2 = ent2.ObjectId;
Entity tmpEnt2 =
(Entity)myT.GetObject(entid2, OpenMode.ForRead);
CurveObj2 = tmpEnt2 as Curve;
if (CurveObj2 != null)
{
CurveType =
CurveObj2.GetType().Name;
if (CurveType == "Polyline" ||
CurveType == "Spline" ||
CurveType == "Line" ||
CurveType == "Arc" || CurveType == "Ellipse")
{
RightObject2 = true;

}
else
{
entopts2.Message = "Falsches
Objekt!\nUnterkante zeigen";
}
}
}

} while (RightObject2 != true);

tmpEnt1.Unhighlight(path, false);

}
else
{
entopts1.Message = "Falsches
Objekt!\nOberkante zeigen";
}
}

}

} while (RightObject1 != true);




PromptDistanceOptions optDist = new
PromptDistanceOptions("Abstand zeigen");

optDist.AllowNegative = false;
optDist.AllowZero = false;
optDist.AllowNone = false;
optDist.UseDashedLine = true;
if (!m_firstTime2Lines || !m_firstTime1Line)
optDist.DefaultValue = Dist;

PromptDoubleResult resDist = ed.GetDistance(optDist);

if (resDist.Status != PromptStatus.OK)
return;


PromptDoubleOptions optLong = new PromptDoubleOptions("Länge
der langen Böschungslinien in %");

optLong.AllowNegative = false;
optLong.AllowZero = false;
optLong.AllowNone = false;
if (!m_firstTime2Lines)
optLong.DefaultValue = LengthLong;
else
optLong.DefaultValue = 100;

PromptDoubleResult resLong = ed.GetDouble(optLong);

if (resLong.Status != PromptStatus.OK)
return;


PromptDoubleOptions optShort = new
PromptDoubleOptions("Länge der kurzen Böschungslinien in %");

optShort.AllowNegative = false;
optShort.AllowZero = false;
optShort.AllowNone = false;
if (!m_firstTime2Lines)
optShort.DefaultValue = LengthShort;
else
optShort.DefaultValue = 50;

PromptDoubleResult resShort = ed.GetDouble(optShort);

if (resShort.Status == PromptStatus.OK)
{
Dist = resDist.Value;
LengthLong = resLong.Value;
LengthShort = resShort.Value;
}
else
{
return;
}

m_firstTime2Lines = false;



double DistGes = Dist;
double CurveObj1Length =
CurveObj1.GetDistanceAtParameter(CurveObj1.EndParam);
DBObjectCollection Boeschung = new DBObjectCollection();
ObjectIdCollection IDColl = new ObjectIdCollection();

ed.WriteMessage("Schritt1");
BlockTable bt = (BlockTable)myT.GetObject(db.BlockTableId,
OpenMode.ForRead, false);
ed.WriteMessage("Schritt2");

//HERE IS SOME MISTAKE --- I ALWAYS GET AN ERROR, WHY???
//BlockTableRecord btr =
(BlockTableRecord)myT.GetObject(CurveObj1.OwnerId, OpenMode.ForWrite);
BlockTableRecord btr =
(BlockTableRecord)myT.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);


ed.WriteMessage("Schritt3");
bool IsLongLine = true;

do
{

Point3d Obj1Punkt = CurveObj1.GetPointAtDist(DistGes);
Point3d Obj2Punkt;
//double Obj1Param =
CurveObj1.GetParameterAtDistance(DistGes);
Vector3d Obj1Deriative1 =
CurveObj1.GetFirstDerivative(Obj1Punkt);


Xline XTemp = new Xline();
XTemp.UnitDir = new Vector3d(Obj1Deriative1.Y,
Obj1Deriative1.X * -1, Obj1Deriative1.Z);
XTemp.BasePoint = Obj1Punkt;

Point3dCollection SPunkte = new Point3dCollection();
XTemp.IntersectWith(CurveObj2, Intersect.OnBothOperands,
SPunkte, 0, 0);

if (SPunkte.Count > 0)
{
double DistTemp = 0;
int z = 0;
foreach (Point3d tmpPt in SPunkte)
{
if (z == 0)
{
DistTemp = Obj1Punkt.DistanceTo(tmpPt);
Obj2Punkt = tmpPt;
z++;
}
else
{
if (Obj1Punkt.DistanceTo(tmpPt) < DistTemp)
{
DistTemp = Obj1Punkt.DistanceTo(tmpPt);
Obj2Punkt = tmpPt;
}
}
}
//XTemp.Cancel();

Line Boeschungslinie = new Line(Obj1Punkt,
Obj2Punkt);

double Length;
if (IsLongLine)
Length = LengthLong * 0.01;
else
Length = LengthShort * 0.01;

Matrix3d scaleMatrix = Matrix3d.Scaling(Length,
Obj1Punkt);


if (Length != 1)
{
Boeschungslinie.TransformBy(scaleMatrix);
}

IsLongLine = (IsLongLine == true) ? false : true;

btr.AppendEntity(Boeschungslinie);
myT.AddNewlyCreatedDBObject(Boeschungslinie, true);
}

DistGes += Dist;
} while (DistGes < CurveObj1Length);

myT.Commit();
}
catch (System.Exception)
{
myT.Commit();
throw;
}

finally
{
myT.Dispose();
}

}
}
}[/code]

Roland
Message 4 of 7
Anonymous
in reply to: Anonymous

Thank you Albert, i will do so the next time.
Maybe i explained it a little bit wrong.
I do not want to make the "CurveObj1" and "CurveObj2" selectable afterwords, i want to make the objects "Boeschungslinie" selectable.

Roland
Message 5 of 7
Anonymous
in reply to: Anonymous

There's no good/easy way to do this in any of our APIs. (I hope someone can
correct me.) The only thing I can think of is to do Editor.SelectXXX to
select the newly created objects. So they are available to select previous
next time.

Albert

wrote in message news:4867082@discussion.autodesk.com...
Thank you Albert, i will do so the next time.
Maybe i explained it a little bit wrong.
I do not want to make the "CurveObj1" and "CurveObj2" selectable afterwords,
i want to make the objects "Boeschungslinie" selectable.

Roland
Message 6 of 7
Anonymous
in reply to: Anonymous

Ok, but is it possible to select the ObjectId's and put them in the SelectionSet?

Roland
Message 7 of 7
Anonymous
in reply to: Anonymous

Not in a way that would allow selection previous to work.

Albert
wrote in message news:4867086@discussion.autodesk.com...
Ok, but is it possible to select the ObjectId's and put them in the
SelectionSet?

Roland

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