#region "Helper Functions"
#region "Associative Framework Helper Functions"
///
//FullSubentityPath
// //
// This .NET class wraps the AcDbFullSubentPath ObjectARX class. This class is used for uniquely identifying //
// a subentity within a particular entity. An object of this class consists of array of object IDs and an //
// SubentityId object. The subentity ID object contains the graphics system marker of the subentity and the //
// type of the subentity (that is, edge, face, vertex). The object ID array contains a sequential list of the //
// object IDs for all the objects that make up the "path" from the outermost entity (in Model or Paper space) down //
// into the "main" entity that the subentity is a part of. If the subentity's "main" entity is directly owned by the //
// Model Space or Paper Space BlockTableRecords then the object ID array will have only one entry--the object ID of the //
// "main" entity itself. For example, an edge of an Solid3d that's within a block definition that's referenced by an //
// BlockReference would result in an object ID array with two object IDs. The first would be the object ID of the //
// BlockReference. The second would be the object ID of the Solid3d //
///
///
private FullSubentityPath GetFullSubentityPath(Entity entity, SubentityType subentityType)
{
// Create Protocol extension to query subentities
AssocPersSubentityIdPE subentityIdPE = GetSubEntIdProtocolExt(entity);
// Get list of Sub Entites and for a line has one edge
SubentityId[] subentityIds = subentityIdPE.GetAllSubentities(entity, subentityType);
ObjectId[] ids = { entity.ObjectId };
return new FullSubentityPath(ids, subentityIds[0]);
}
///
///Protocol extension//
// //
// //
// Protocol extension is a mechanism for adding functionality to existing ObjectARX classes. This new //
// functionality is embodied in protocol extension classes associated with an existing ObjectARX class //
// at runtime. The association of an ObjectARX class with protocol extension classes is made by way of //
// the ObjectARX class descriptor object, as described in chapter 16, Deriving a Custom ObjectARX Class //
// The class descriptor object describes the class and includes an array of pointers to any objects that //
// extend the functionality of that class. An ObjectARX class can have any number of protocol extensions //
// associated with it. //
// //
///
///
private AssocPersSubentityIdPE GetSubEntIdProtocolExt(Entity ent)
{
AssocPersSubentityIdPE assocsubentityIdPE = default(AssocPersSubentityIdPE);
RXClass peCls = AssocPersSubentityIdPE.GetClass(typeof(AssocPersSubentityIdPE));
IntPtr ptrSubentityIdPE = ent.QueryX(peCls);
//'
assocsubentityIdPE = AssocPersSubentityIdPE.Create(ptrSubentityIdPE, false) as AssocPersSubentityIdPE;
if (assocsubentityIdPE == null)
{
System.Windows.MessageBox.Show("cannot get subentityIdPE");
return null;
}
return assocsubentityIdPE;
}
private Assoc2dConstraintGroup GetConstraintGroup(OpenMode openMode)
{
Plane currentPlane = GetCurrentPlane();
///'Helper Function to calculate Current Plane
Assoc2dConstraintGroup constraintGroup = default(Assoc2dConstraintGroup);
using (Transaction trx = db.TransactionManager.TopTransaction)
{
ObjectId modelSpaceId = GetModelSpaceId();
///' Helper Function to get ModelSpace's ObjectID
ObjectId assocNetwrkId = AssocNetwork.GetInstanceFromObject(modelSpaceId, true, true, "");
AssocNetwork assocNetwrk = (AssocNetwork)trx.GetObject(assocNetwrkId, OpenMode.ForRead);
// Iterate all actions in network to find Assoc2dConstraintGroups
ObjectIdCollection actionIds = assocNetwrk.GetActions;
foreach (ObjectId actionId in actionIds)
{
if (actionId == ObjectId.Null)
{
continue;
}
if (actionId.ObjectClass.Name == "AcDbAssoc2dConstraintGroup")
{
constraintGroup = (Assoc2dConstraintGroup)trx.GetObject(actionId, OpenMode.ForRead);
// Is this the Assoc2dConstraintGroup for our plane of interest?
if (constraintGroup.GetWorkPlane.IsCoplanarTo(currentPlane))
{
if (openMode == OpenMode.ForWrite)
{
constraintGroup.UpgradeOpen();
}
return constraintGroup;
}
}
}
// If we get to here, a suitable contraint group doesn't exist, create a new one if that's what calling fn wanted.
assocNetwrk.UpgradeOpen();
// If model extent is far far away from origin then we need to shift
// construction plane origin within the model extent.
// (Use Pextmin, PExtmax in paper space)
Point3d extmin = db.Extmin;
Point3d extmax = db.Extmax;
if (extmin.GetAsVector().Length > 100000000.0)
{
Point3d originL = extmin + (extmax - extmin) / 2.0;
PointOnSurface result = currentPlane.GetClosestPointTo(originL);
currentPlane.Set(result.GetPoint(), currentPlane.Normal);
}
// Create the new constraint group and add it to the associative network.
constraintGroup = new Assoc2dConstraintGroup(currentPlane);
ObjectId constraintGroupId = db.AddDBObject(constraintGroup);
assocNetwrk.AddAction(constraintGroupId, true);
}
return constraintGroup;
}
private Plane GetCurrentPlane()
{
// Calculate the current plane on which new entities are added by the editor
// (A combination of UCS and ELEVATION sysvar).
Matrix3d ucsMatrix = ed.CurrentUserCoordinateSystem;
Point3d origin = ucsMatrix.CoordinateSystem3d.Origin;
Vector3d xAxis = ucsMatrix.CoordinateSystem3d.Xaxis;
Vector3d yAxis = ucsMatrix.CoordinateSystem3d.Yaxis;
Vector3d zAxis = ucsMatrix.CoordinateSystem3d.Zaxis;
origin = origin + Convert.ToDouble(Application.GetSystemVariable("ELEVATION")) * zAxis;
return new Plane(origin, xAxis, yAxis);
}
#endregion
#region "Geometry and Model Space Helper Functions"
private ObjectId GetModelSpaceId()
{
Transaction trx = default(Transaction);
if (db.TransactionManager.NumberOfActiveTransactions > 0)
{
trx = db.TransactionManager.TopTransaction;
}
else
{
trx = db.TransactionManager.StartTransaction();
}
using (trx)
{
return SymbolUtilityServices.GetBlockModelSpaceId(db);
}
}
private BlockTableRecord GetModelSpace(OpenMode openMode)
{
Transaction trx = default(Transaction);
if (db.TransactionManager.NumberOfActiveTransactions > 0)
{
trx = db.TransactionManager.TopTransaction;
}
else
{
trx = db.TransactionManager.StartTransaction();
}
using (trx)
{
return (BlockTableRecord)trx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), openMode);
}
}
private Entity CreateRandomEntity(CreateEntityType entType)
{
Transaction trx = default(Transaction);
if (db.TransactionManager.NumberOfActiveTransactions > 0)
{
trx = db.TransactionManager.TopTransaction;
}
else
{
trx = db.TransactionManager.StartTransaction();
}
Entity ent = default(Entity);
using (trx)
{
double x1 = rdn.NextDouble() + rdn.Next(100);
double y1 = rdn.NextDouble() + rdn.Next(100);
double x2 = rdn.NextDouble() + rdn.Next(100);
double y2 = rdn.NextDouble() + rdn.Next(100);
if (entType == CreateEntityType.Line)
{
Line line = new Line(new Point3d(x1, y1, 0), new Point3d(x2, y2, 0));
ent = line;
}
else
{
if (x2 < 2)
{
x2 = x2 + 5;
}
Circle circle = new Circle(new Point3d(x1, y1, 0), Vector3d.ZAxis, x2);
ent = circle;
}
BlockTableRecord modelSpace = GetModelSpace(OpenMode.ForWrite);
ObjectId objId = modelSpace.AppendEntity(ent);
trx.AddNewlyCreatedDBObject(ent, true);
return ent;
}
}
#endregion
#endregion
【高阶CAD二次开发】文档2
最新推荐文章于 2024-07-17 16:41:35 发布