AutocadApplicationGeometry.Point3dCollection ptOneColl = PolyToPoint3dCollection(polyline);
AutocadApplicatioEditorInput.PromptSelectionResult psr = null;
psr = ed.SelectCrossingPolygon(ptOneColl);
if (psr.Status == AutocadApplicatioEditorInput.PromptStatus.OK)
{
foreach (ObjectId objectId in psr.Value.GetObjectIds())
{
try {
//ToDo изменить as на is
hatch = transaction.GetObject(objectId, OpenMode.ForRead) as Hatch;
} catch(System.Exception ex) {
continue;
}
if (hatch == null)
continue;
try
{
if (targetObjectIds.Keys.Contains(hatch.PatternName))
targetObjectIds[hatch.PatternName].Count = targetObjectIds[hatch.PatternName].Count + hatch.Area;
else
{
BlockTable acBlkTbl;
acBlkTbl = transaction.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = transaction.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
//// Создание круга для замкнутого контура штриховки
Circle acCirc = new Circle();
acCirc.SetDatabaseDefaults();
acCirc.Center = new AutocadApplicationGeometry.Point3d(3, 3, 0);
acCirc.Radius = 1;
//// Добавление нового объекта круга в запись таблицы блоков и в транзакцию
acBlkTblRec.AppendEntity(acCirc);
transaction.AddNewlyCreatedDBObject(acCirc, true);
//// Добавление круга в массив объектов
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acCirc.ObjectId);
//// Создание штриховки и добавление её в запись таблицы блоков и в транзакцию
//Hatch acHatch = (Hatch) hatch.Clone();
//Hatch oHatch = new Hatch();
Hatch oHatch = (Hatch)hatch.Clone();
int count = oHatch.NumberOfLoops;
for (int i = 0; i < count; i++)
oHatch.RemoveLoopAt(0);
//AutocadApplicationGeometry.Vector3d normal = new AutocadApplicationGeometry.Vector3d(0.0, 0.0, 1.0);
//oHatch.Normal = normal;
//oHatch.Elevation = 0.0;
//oHatch.PatternScale = 2.0;
//oHatch.SetHatchPattern(HatchPatternType.PreDefined, "ZIGZAG");
//oHatch.ColorIndex = 1;
acBlkTblRec.AppendEntity(oHatch);
transaction.AddNewlyCreatedDBObject(oHatch, true);
//this works ok
oHatch.Associative = true;
oHatch.AppendLoop((int)HatchLoopTypes.Default, acObjIdColl);
oHatch.EvaluateHatch(true);
//acBlkTblRec.AppendEntity(acHatch);
//transaction.AddNewlyCreatedDBObject(acHatch, true);
// Установка свойтсв штриховки
// Ассоциативность должна быть установлена после добавления штриховки в запись таблицы блоков и до метода AppendLoop
// acHatch.SetDatabaseDefaults();
//acHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
//acHatch.Associative = true;
//acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
// acHatch.EvaluateHatch(true);
// Сохранение нового объекта в базу данных
//transaction.AddNewlyCreatedDBObject(acBlkTblRec, true);
transaction.Commit();
//Hatch ha = new Hatch();
//ha.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
//ha.Normal = AutocadApplicationGeometry.Vector3d.ZAxis;
//ha.AppendLoop(HatchLoopTypes.Default, acObjIdColl);
//ha.EvaluateHatch(true);
//transaction.Commit();
using (Transaction transaction2 = db.TransactionManager.StartTransaction())
{
//BlockTableRecord hatchBlockTableRecord;
//hatchBlockTableRecord = transaction2.GetObject(acHatch.ObjectId,
// OpenMode.ForRead) as BlockTableRecord;
BlockTable bt = (BlockTable)transaction2.GetObject(
db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord br = new BlockTableRecord();
br.Name = "hatch" + Guid.NewGuid().ToString();
br.Origin = new AutocadApplicationGeometry.Point3d(10.0, 10.0, 10.0);
//foreach (var id in ids)
//{
Entity ent = (Entity)transaction2.GetObject(oHatch.ObjectId, OpenMode.ForRead);
br.AppendEntity(ent.Clone() as Entity);
// br.AppendEntity(ent);
//if (eraseEntities)
//{
ent.UpgradeOpen();
ent.Erase(true);
//}
//}
bt.Add(br);
transaction2.AddNewlyCreatedDBObject(br, true);
transaction2.Commit();
ResultBuffer buf =
new ResultBuffer(new TypedValue((int)DxfCode.Handle, br.ObjectId.Handle));
//Retrieve the Handle and use it to construct an ObjectId
TypedValue[] arr = buf.AsArray();
long val = Convert.ToInt64(arr[0].Value.ToString(), 16);
newItem = new ItemForCalculate();
newItem.Count = hatch.Area;
newItem.ObjectId = Convert.ToString(val);
targetObjectIds.Add(hatch.PatternName, newItem);
}
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
Console.Write(ex.Message);
}
}
}