الحصول على حجم التخطيط
Contents
[
Hide
]**كيفية الحصول على حجم التخطيط **
المشكلة: كيفية الحصول على حجم التخطيط (CADNET-438).
نصائح: للقيام بذلك، يوضح المثال أدناه كيفية حساب الحجم للطبقات غير الفارغة لتنسيقات الملفات المختلفة.
مثال:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string extension = Path.GetExtension(fileName); | |
using (CadImage cadImage = (CadImage)Image.Load(fileName)) | |
{ | |
List<string> layouts = GetNotEmptyLayouts(cadImage, extension); | |
const double Epsilon = 0.00001; | |
double dpi = 300; | |
foreach (string layout in layouts) | |
{ | |
System.Console.WriteLine("Layout= " + layout); | |
using (FileStream fs = new FileStream(outDir + "layout_" + layout + ".jpg", FileMode.Create)) | |
{ | |
JpegOptions jpegOptions = new JpegOptions(); | |
CadRasterizationOptions options = new CadRasterizationOptions(); | |
options.Layouts = new string[] { layout }; | |
CadLayout l = cadImage.Layouts[layout]; | |
if ((Math.Abs(l.MaxExtents.Y) < Epsilon && Math.Abs(l.MaxExtents.X) < Epsilon) || (Math.Abs(l.MaxExtents.Y + 1E+20) < Epsilon || Math.Abs(l.MaxExtents.X + 1E+20) < Epsilon) || (Math.Abs(l.MinExtents.Y - 1E+20) < Epsilon || Math.Abs(l.MinExtents.X - 1E+20) < Epsilon)) | |
{ | |
// do nothing, we can automatically detect size | |
// we can not rely on PlotPaperUnits here too because it is PlotInInches by default | |
} | |
else | |
{ | |
double sizeExtX = l.MaxExtents.X - l.MinExtents.X; | |
double sizeExtY = l.MaxExtents.Y - l.MinExtents.Y; | |
if (l.PlotPaperUnits == CadPlotPaperUnits.PlotInInches) | |
{ | |
options.PageHeight = INtoPixels(sizeExtY, dpi); | |
options.PageWidth = INtoPixels(sizeExtX, dpi); | |
} | |
else | |
{ | |
if (l.PlotPaperUnits == CadPlotPaperUnits.PlotInMillimeters) | |
{ | |
options.PageHeight = MMtoPixels(sizeExtY, dpi); | |
options.PageWidth = MMtoPixels(sizeExtX, dpi); | |
} | |
else | |
{ | |
options.PageHeight = (float)sizeExtY; | |
options.PageWidth = (float)sizeExtX; | |
} | |
} | |
} | |
System.Console.WriteLine("Layout= " + layout + " height= " + options.PageHeight + " width= " + options.PageWidth); | |
jpegOptions.VectorRasterizationOptions = options; | |
cadImage.Save(fs, jpegOptions); | |
} | |
} | |
} | |
private static List<string> GetNotEmptyLayouts(Image cadImage, string extension) | |
{ | |
List<string> nonEmptyLayouts = new List<string>(); | |
if (cadImage == null) | |
return nonEmptyLayouts; | |
switch (extension) | |
{ | |
case ".dwg": | |
nonEmptyLayouts = GetNotEmptyLayoutsForDwg((CadImage)cadImage); | |
break; | |
case ".dxf": | |
nonEmptyLayouts = GetNotEmptyLayoutsForDxf((CadImage)cadImage); | |
break; | |
} | |
return nonEmptyLayouts; | |
} | |
private static int MMtoPixels(double mm, double dpi) | |
{ | |
double inches = mm * 0.0393701; | |
double pixels = dpi * inches; | |
return (int)(pixels + 0.5); | |
} | |
private static int INtoPixels(double inches, double dpi) | |
{ | |
double pixels = dpi * inches; | |
return (int)(pixels + 0.5); | |
} | |
private static List<string> GetNotEmptyLayoutsForDxf(CadImage cadImage) | |
{ | |
List<string> notEmptyLayouts = new List<string>(); | |
Dictionary<string, string> layoutBlockHandles = new Dictionary<string, string>(); | |
foreach (CadLayout layout in cadImage.Layouts.Values) | |
{ | |
if (layout.BlockTableRecordHandle != null) | |
layoutBlockHandles.Add(layout.BlockTableRecordHandle, layout.LayoutName); | |
} | |
foreach (CadBaseEntity entity in cadImage.Entities) | |
{ | |
if (layoutBlockHandles.ContainsKey(entity.SoftOwner)) | |
{ | |
string layoutName = layoutBlockHandles[entity.SoftOwner]; | |
if (!notEmptyLayouts.Contains(layoutName)) | |
notEmptyLayouts.Add(layoutName); | |
} | |
} | |
return notEmptyLayouts; | |
} | |
private static List<string> GetNotEmptyLayoutsForDwg(CadImage cadImage) | |
{ | |
List<string> notEmptyLayouts = new List<string>(); | |
foreach (CadLayout layout in cadImage.Layouts.Values) | |
{ | |
foreach (CadBlockTableObject tableObject in cadImage.BlocksTables) | |
{ | |
if (string.Equals(tableObject.HardPointerToLayout, layout.ObjectHandle)) | |
{ | |
if (cadImage.BlockEntities.ContainsKey(tableObject.BlockName)) | |
{ | |
CadBlockEntity cadBlockEntity = cadImage.BlockEntities[tableObject.BlockName]; | |
if (cadBlockEntity.Entities.Length > 0) | |
notEmptyLayouts.Add(layout.LayoutName); | |
} | |
break; | |
} | |
} | |
} | |
return notEmptyLayouts; | |
} |