[CommandMethod("TestHatchDxf")]
public void TestHatchDxf()
{
Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions entityOpt = new PromptEntityOptions("\nSelect hatch: ");
PromptEntityResult entityRes = ed.GetEntity(entityOpt);
if (entityRes.Status == PromptStatus.OK)
{
ObjectId id = entityRes.ObjectId;
if (!id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Hatch))))
{
MessageBox.Show("Не шртиховка");
return;
}
db.Dimzin = 0;
List<TypedValue> dxf = new List<TypedValue>();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
using (Hatch hatch = tr.GetObject(entityRes.ObjectId, OpenMode.ForWrite, false, true) as Hatch)
{
double angle = hatch.PatternAngle;
try { hatch.PatternAngle = 0; } catch { }
long[] ent = new long[] { 0, 0 };
acdbGetAdsName(ent, id);
using (ResultBuffer rb = DisposableWrapper.Create(typeof(ResultBuffer), acdbEntGet(ent), true) as ResultBuffer)
{
if (rb != null) dxf = rb.AsArray().ToList();
}
try { hatch.PatternAngle = angle; } catch { }
}
tr.Commit();
}
if (dxf.Count > 0)
{
string pattern = (string)dxf.Where(tv => tv.TypeCode == 2).Select(tv => tv.Value).FirstOrDefault();
double rotation = (double)dxf.Where(tv => tv.TypeCode == 52).Select(tv => tv.Value).FirstOrDefault() * 180 / Math.PI;
double scale = (double)dxf.Where(tv => tv.TypeCode == 41).Select(tv => tv.Value).FirstOrDefault();
double xoffset = (double)dxf.Where(tv => tv.TypeCode == 43).Select(tv => tv.Value).FirstOrDefault();
double yoffset = (double)dxf.Where(tv => tv.TypeCode == 44).Select(tv => tv.Value).FirstOrDefault();
string record = "*" + pattern;
TypedValue member = dxf.Where(tv => tv.TypeCode == 53).FirstOrDefault();
int index = dxf.IndexOf(member);
dxf.RemoveRange(0, index);
string filePath = Path.Combine((string)AcAp.Application.GetSystemVariable("DWGPREFIX"), pattern + ".pat");
if (File.Exists(filePath)) File.Delete(filePath);
double angl = 0d, tmp1 = 0d, tmp2 = 0d, xvec = 0d, yvec = 0d;
using (StreamWriter file = new StreamWriter(filePath, false, Encoding.Default))
{
foreach (TypedValue val in dxf)
{
switch (val.TypeCode)
{
case 53:
file.WriteLine(record);
angl = (double)val.Value;
record = string.Format("{0:F7}", angl * 180 / Math.PI).TrimEnd('0').TrimEnd('.');
break;
case 43:
record += ", " + string.Format("{0:F7}", (((double)val.Value) - xoffset) / scale).TrimEnd('0').TrimEnd('.');
break;
case 44:
record += "," + string.Format("{0:F7}", (((double)val.Value) - yoffset) / scale).TrimEnd('0').TrimEnd('.');
break;
case 45:
tmp1 = (double)val.Value;
break;
case 46:
tmp2 = (double)val.Value;
xvec = (tmp1 * Math.Cos(angl) + tmp2 * Math.Sin(angl)) / scale;
yvec = (tmp2 * Math.Cos(angl) - tmp1 * Math.Sin(angl)) / scale;
record += ", " + string.Format("{0:F7}", xvec).TrimEnd('0').TrimEnd('.') + "," +
string.Format("{0:F7}", yvec).TrimEnd('0').TrimEnd('.');
break;
case 49:
record += ", " + string.Format("{0:F7}", (double)val.Value / scale).TrimEnd('0').TrimEnd('.');
break;
case 98:
file.WriteLine(record);
break;
default:
break;
}
}
file.WriteLine("");
}
}
}
}