แปลง DWG DXF เป็น PDF C# | แปลงไฟล์ Auto CAD เป็น PDF JPEG PNG ใน C# .NET

แปลง DWG หรือ DXF เป็น PNG JPEG BMP GIF หรือ TIFF ใน C#

Aspose.CAD สำหรับ .NET สามารถแปลงรูปแบบการวาด AutoCAD เช่น DXF และ DWG เป็น PNG, BMP, TIFF, JPEG และ GIF ได้ มันมี API ที่มีประสิทธิภาพและใช้งานง่ายเพื่อบรรลุเป้าหมายนี้

คุณสามารถแปลงรูปแบบการวาด AutoCAD ที่รองรับทั้งหมดเป็นรูปแบบภาพเรเซอร์ด้วยขั้นตอนง่ายๆ ตามที่ได้กล่าวไว้ด้านล่างนี้

  1. โหลดไฟล์ AutoCAD DWG หรือ DXF ลงในคลาส Image.
  2. สร้างอินสแตนซ์ของ CadRasterizationOptions.
  3. ตั้ง/เปลี่ยนขนาดของภาพโดยใช้ PageWidth และ PageHeight
  4. สร้างอินสแตนซ์ของ ImageOptionsBase
  5. ตั้งค่าคุณสมบัติ VectorRasterizationOptions เป็น CadRasterizationOptions ที่สร้างในขั้นตอนก่อนหน้า
  6. บันทึกการวาด AutoCAD เป็น PDF ด้วย Image.Save โดยการส่งเส้นทางไฟล์ (หรือวัตถุ MemoryStream) รวมถึงอินสแตนซ์ของ ImageOptionsBase ที่สร้างในขั้นตอนก่อนหน้า

นี่คือโค้ดต้นฉบับทั้งหมด

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Create an instance of PngOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.PngOptions();
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_raster_image_out.png";
// Save resultant image
image.Save(MyDir, options);
}

โดยค่าเริ่มต้น, API จะแสดงเฉพาะเลย์เอาต์ “Model” อย่างไรก็ตามคุณสามารถระบุเลย์เอาต์ที่คุณเลือกขณะแปลงการวาด CAD เป็นรูปแบบภาพ

ปรับแต่งการแปลง CAD

ขั้นตอนการแปลง CAD เป็น PDF & การแปลง CAD เป็นรูปภาพเรเซอร์นั้นสามารถปรับแต่งได้อย่างมาก เนื่องจากคลาส CadRasterizationOptions ถูกนำไปใช้อย่างสามารถปรับแต่งได้มากมาย ซึ่งเมื่อมีการตั้งค่าก็สามารถควบคุมกระบวนการเรนเดอร์ตามความต้องการของแอปพลิเคชันได้

คลาส CadRasterizationOptions

คลาส CadRasterizationOptions เป็นมาตรฐานสำหรับรูปแบบ CAD ที่รองรับทั้งหมด เช่น DWG & DXF ดังนั้น ข้อมูลที่แบ่งปันในบทความนี้จึงถูกต้องสำหรับรูปแบบ CAD ทั้งสองประเภทข้างต้น

คุณสมบัติของคลาส CadRasterizationOptions ที่มีประโยชน์คือ:

PropertyDefault ValueRequiredDescription
PageWidth0ใช่ระบุความกว้างของหน้า
PageHeight0ใช่ระบุความสูงของหน้า
ScaleMethodScaleType.ShrinkToFitไม่ระบุว่าการวาดควรถูกปรับขนาดโดยอัตโนมัติหรือไม่ ค่าเริ่มต้นจะย่อภาพให้อยู่ในขนาดผืนผ้าใบ สลับไปที่โหมด GrowToFit หรือใช้การตั้งค่า None เพื่อปิดการปรับขนาดอัตโนมัติ
BackgroundColorColor.Whiteไม่ระบุสีพื้นหลังของภาพที่ส่งออก
DrawTypeCadDrawTypeMode.UseDrawColorไม่ระบุโหมดการระบายสีของเอนทิตี ระบุตัวเลือก UseObjectColor เพื่อวาดเอนทิตีโดยใช้สีของมัน หรือใช้ตัวเลือก UseDrawColor เพื่อเขียนทับสีดั้งเดิม
DrawColorColor.Blackไม่ระบุสีที่ถูกเขียนทับของเอนทิตี (เฉพาะเมื่อ DrawType ถูกตั้งค่าเป็นค่าของตัวแปร UseDrawColor)
AutomaticLayoutsScalingFalseไม่ระบุว่าต้องทำการปรับขนาดเลย์เอาต์อัตโนมัติให้ตรงกับเลย์เอาต์โมเดลหรือไม่

การตั้งค่าขนาดและโหมดผืนผ้าใบ

การส่งออกจาก CAD ไปยัง PDF หรือรูปภาพเรเซอร์เป็นงานที่ไม่ธรรมดา เนื่องจาก PDF หรือภาพที่ได้จะต้องกำหนดขนาดผืนผ้าใบ เราจึงต้องระบุมิติของหน้าสำหรับ PDF เพื่อให้การวาดแสดงผลอย่างถูกต้อง ตั้งค่าคุณสมบัติ CadRasterizationOptions.PageWidth และ CadRasterizationOptions.PageHeight อย่างชัดเจน หรือคุณอาจพบ ImageSaveException

นอกจากนี้คุณยังสามารถกำหนดตัวเลือกการปรับขนาดมิติได้ ตัวเลือกการปรับขนาดถูกตั้งค่าด้วยคุณสมบัติ CadRasterizationOptions.ScaleMethod ใช้ตัวเลือกนี้เพื่อปรับขนาดของภาพตามค่าของ CadRasterizationOptions.PageWidth และ CadRasterizationOptions.PageHeight โดยค่าเริ่มต้น CadRasterizationOptions.ScaleMethod จะตั้งค่าเป็นโหมด ScaleType.ShrinkToFit คุณสมบัตินี้จะกำหนดพฤติกรรมดังต่อไปนี้:

  • หากขนาดการวาด CAD ใหญ่กว่าขนาดผืนผ้าใบผลลัพธ์ ขนาดการวาดจะถูกลดขนาดเพื่อลงในผืนผ้าใบโดยรักษาสัดส่วน
  • หากขนาดการวาด CAD เล็กกว่าขนาดผืนผ้าใบผลลัพธ์ ให้ตั้งค่าคุณสมบัติ CadRasterizationOptions.ScaleMethod เป็น ScaleType.GrowToFit เพื่อเพิ่มขนาดการวาดให้พอดีกับผืนผ้าใบ PDF โดยรักษาสัดส่วน
  • หรือปิดการปรับขนาดอัตโนมัติด้วยตัวเลือก ScaleType.None

โค้ดตัวอย่างด้านล่างแสดงการใช้ตัวเลือกการปรับขนาดอัตโนมัติ

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.AutomaticLayoutsScaling = true;
rasterizationOptions.NoScaling = false;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

การตั้งค่าพื้นหลังและสีการวาด

โดยค่าเริ่มต้น แผนภูมิสีสำหรับผืนผ้าใบผลลัพธ์จะตั้งค่าเป็นแผนผังเอกสารทั่วไป ซึ่งหมายความว่าทุกเอนทิตีภายในการวาด CAD จะถูกวาดด้วยปากกาสีดำบนพื้นหลังสีขาวแข็ง สถานะเหล่านี้สามารถเปลี่ยนแปลงได้ด้วยคุณสมบัติ CadRasterizationOptions.BackgroundColor และ CadRasterizationOptions.DrawColor การเปลี่ยนคุณสมบัติ CadRasterizationOptions.DrawColor ยังต้องมีการตั้งค่าคุณสมบัติ CadRasterizationOptions.DrawType เพื่อใช้สีการวาดที่ใช้ คุณสมบัติ CadRasterizationOptions.DrawType กำหนดว่าเอนทิตี CAD จะรักษาสีของมันหรือถูกแปลงเป็นสีที่กำหนดเอง เพื่อรักษาสีของเอนทิตี ให้ตั้งค่าคุณสมบัติ CadRasterizationOptions.DrawType เป็น CadDrawTypeMode.UseObjectColor มิฉะนั้นให้ระบุค่า CadDrawTypeMode.UseDrawColor

โค้ดตัวอย่างด้านล่างแสดงวิธีการใช้คุณสมบัติสีที่แตกต่างกัน

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
rasterizationOptions.BackgroundColor = Aspose.CAD.Color.Beige;
rasterizationOptions.DrawType = Aspose.CAD.FileFormats.Cad.CadDrawTypeMode.UseDrawColor;
rasterizationOptions.DrawColor = Aspose.CAD.Color.Blue;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to PDF
image.Save(MyDir + "result_out.pdf", pdfOptions);
// Create an instance of TiffOptions
Aspose.CAD.ImageOptions.TiffOptions tiffOptions = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set the VectorRasterizationOptions property
tiffOptions.VectorRasterizationOptions = rasterizationOptions;
//Export CAD to TIFF
image.Save(MyDir + "result_out.tiff", tiffOptions);
}

การตั้งค่าการปรับขนาดเลย์เอาต์อัตโนมัติ

การวาด CAD ส่วนใหญ่มีเลย์เอาต์มากกว่าหนึ่งชุดที่เก็บไว้ในไฟล์เดียว และแต่ละเลย์เอาต์อาจมีขนาดที่แตกต่างกัน ในขณะที่เรนเดอร์การวาด CAD เหล่านั้นเป็น PDF แต่ละหน้าของ PDF อาจมีการปรับขนาดที่แตกต่างกันตามขนาดเลย์เอาต์ เพื่อให้การเรนเดอร์มีความเป็นเอกลักษณ์ API ของ Aspose.CAD ได้เปิดเผยคุณสมบัติ CadRasterizationOptions.AutomaticLayoutsScaling ค่าเริ่มต้นคือ false แต่เมื่อเป็นจริง API จะพยายามค้นหาอัตราส่วนที่สอดคล้องกันสำหรับแต่ละเลย์เอาต์แยกต่างหากและวาดพวกเขาอย่างเหมาะสมด้วยการทำการปรับขนาดอัตโนมัติตามขนาดหน้า

นี่คือวิธีการทำงานของคุณสมบัติ CadRasterizationOptions.AutomaticLayoutsScaling ร่วมกับคุณสมบัติ CadRasterizationOptions.ScaleMethod

  1. ถ้าหาก ScaleMethod ถูกตั้งค่าเป็น ScaleType.ShrinkToFit หรือ ScaleType.GrowToFit พร้อมการตั้งค่าของ AutomaticLayoutsScaling เป็น false แล้วทุกเลย์เอาต์ (รวมถึงโมเดล) จะถูกประมวลผลตามตัวเลือกแรก
  2. ถ้าหาก ScaleMethod ถูกตั้งค่าเป็น ScaleType.ShrinkToFit หรือ ScaleType.GrowToFit พร้อมการตั้งค่าของ AutomaticLayoutsScaling เป็น true แล้วทุกเลย์เอาต์ (ยกเว้นโมเดล) จะถูกประมวลผลตามขนาดขณะที่โมเดลจะถูกประมวลผลตามตัวเลือกแรก
  3. ถ้าหาก ScaleMethod ถูกตั้งค่าเป็น ScaleType.None พร้อมการตั้งค่าของ AutomaticLayoutsScaling เป็น true หรือ false จะไม่มีการปรับขนาดเกิดขึ้น

โค้ดตัวอย่างด้านล่างแสดงวิธีการตั้งค่าการปรับขนาดเลย์เอาต์อัตโนมัติสำหรับการแปลง CAD เป็น PDF

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
rasterizationOptions.PageWidth = 1600;
rasterizationOptions.PageHeight = 1600;
// Set Auto Layout Scaling
rasterizationOptions.AutomaticLayoutsScaling = true;
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "result_out.pdf";
//Export the CAD to PDF
image.Save(MyDir, pdfOptions);
}

แปลงเลย์เอาต์ AutoCAD DXF หรือ DWG เป็น PNG หรือรูปภาพอื่นใน C#

Aspose.CAD สำหรับ .NET API สามารถแปลงเลย์เอาต์ CAD ของรูปแบบที่รองรับ เช่น DXF และ DWG เป็น PNG BMP TIFF JPEG และ GIF API ยังให้การสนับสนุนในการเรนเดอร์เลย์เอาต์เฉพาะของการวาด CAD ลงในเลเยอร์ PSD ที่แตกต่างกัน

นี่คือวิธีการที่คุณสามารถบรรลุสิ่งเดียวกันนี้ในขั้นตอนที่ง่ายดายดังต่อไปนี้

  • โหลดไฟล์ AutoCAD DWG หรือ DXF โดยใช้คลาส Image
  • ตั้ง/เปลี่ยนความกว้างและความสูงของภาพ
  • ตั้งชื่อเลย์เอาต์ที่ต้องการโดยใช้คุณสมบัติ CadRasterizationOptions.Layouts
  • สร้างอินสแตนซ์ของ ImageOptionsBase และตั้งค่าคุณสมบัติ VectorRasterizationOptions เป็นอินสแตนซ์ของ CadRasterizationOptions ที่สร้างในขั้นตอนก่อนหน้า
  • บันทึกเลย์เอาต์ CAD เป็น TIFF หรือรูปภาพ

นี่คือโค้ดต้นฉบับทั้งหมด

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set page width & height
rasterizationOptions.PageWidth = 1200;
rasterizationOptions.PageHeight = 1200;
// Specify a list of layout names
rasterizationOptions.Layouts = new string[] { "Model", "Layout1" };
// Create an instance of TiffOptions for the resultant image
ImageOptionsBase options = new Aspose.CAD.ImageOptions.TiffOptions(Aspose.CAD.FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
// Set rasterization options
options.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "conic_pyramid_layoutstorasterimage_out.tiff";
// Save resultant image
image.Save(MyDir, options);
}

เปิดใช้งานการติดตามกระบวนการเรนเดอร์ CAD

Aspose.CAD ได้แนะนำชุดของคลาสและฟิลด์การระบุที่รองรับเพื่อช่วยในการติดตามกระบวนการเรนเดอร์ CAD ด้วยการเปลี่ยนแปลงเหล่านี้ การแปลง CAD เป็น PDF สามารถทำได้ดังต่อไปนี้ในขณะที่เปิดใช้งานการติดตาม

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
MemoryStream stream = new MemoryStream();
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Create an instance of CadRasterizationOptions and set its various properties
Aspose.CAD.ImageOptions.CadRasterizationOptions cadRasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
pdfOptions.VectorRasterizationOptions = cadRasterizationOptions;
cadRasterizationOptions.PageWidth = 800;
cadRasterizationOptions.PageHeight = 600;
image.Save(stream, pdfOptions);
}

การติดตามกระบวนการเรนเดอร์ CAD สามารถตรวจจับปัญหาได้ดังนี้

  1. ข้อมูลหัวข้อที่ขาดหายไปหรือเสียหาย
  2. ข้อมูลเลย์เอาต์ที่ขาดหายไป
  3. เอนทิตีบล็อคที่ขาดหายไป
  4. รูปแบบขนาดที่ขาดหายไป
  5. รูปแบบที่ขาดหายไป

การแทนที่ฟอนต์ขณะแปลงการวาด CAD

อาจเป็นไปได้ว่าการวาด CAD บางอย่างใช้ฟอนต์เฉพาะที่ไม่มีอยู่ในเครื่องที่ทำการแปลง CAD เป็น PDF หรือ CAD เป็นรูปภาพเรเซอร์ ในสถานการณ์เช่นนี้ API ของ Aspose.CAD จะกระตุ้นให้มีข้อยกเว้นที่เหมาะสมเพื่อเน้นฟอนต์ที่ขาดหายไปและหยุดกระบวนการแปลง เนื่องจาก API ต้องการฟอนต์เหล่านี้เพื่อแสดงเนื้อหาอย่างถูกต้องไปยัง PDF หรือรูปภาพที่ส่งออก

Aspose.CAD API มีวิธีที่ง่ายต่อการใช้กลไกในการแทนที่ฟอนต์ที่จำเป็นด้วยฟอนต์ที่มีอยู่ คุณสมบัติ CadImage.Styles คืนค่าอินสแตนซ์ของ CadStylesDictionary ซึ่งมี CadStyleTableObject สำหรับแต่ละสไตล์ในการวาด CAD ขณะที่ CadStyleTableObject.PrimaryFontName สามารถใช้เพื่อระบุชื่อฟอนต์ที่มีอยู่

โค้ดตัวอย่างต่อไปนี้แสดงการใช้งานของ Aspose.CAD สำหรับ .NET API เพื่อเปลี่ยนฟอนต์ของทุกรูปแบบในการวาด CAD

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
// Set the font name
style.PrimaryFontName = "Arial";
}
}
Console.WriteLine("\nFont changed successfully.");
}
public static void SubstitutingFontByName()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}
}

นอกจากนี้ยังสามารถเปลี่ยนฟอนต์ของรูปแบบเฉพาะโดยเข้าถึงผ่านชื่อสไตล์ โค้ดตัวอย่างต่อไปนี้แสดงการใช้งานวิธีนี้

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Iterate over the items of CadStyleDictionary
foreach (CadStyleTableObject style in cadImage.Styles)
{
if (style.StyleName == "Roman")
{
// Specify the font for one particular style
style.PrimaryFontName = "Arial";
}
}
}

การแปลงเลเยอร์ CAD เป็นรูปภาพเรเซอร์รูปแบบ

Aspose.CAD สำหรับ .NET API ได้มีวิธีการที่มีประสิทธิภาพและใช้งานง่ายเพื่อระบุชื่อของเลเยอร์ CAD ที่ต้องการและเรนเดอร์มันเป็นรูปภาพเรเซอร์ ในที่นี้คือการทำใน 5 ขั้นตอนง่ายๆ ดังที่ระบุไว้ด้านล่างนี้

  1. โหลดไฟล์ CAD ลงในอินสแตนซ์ของ Image โดยใช้วิธี Load
  2. สร้างอินสแตนซ์ของ CadRasterizationOptions และตั้งค่าคุณสมบัติที่จำเป็น เช่น PageWidth & PageHeight
  3. เพิ่มชื่อเลเยอร์ที่ต้องการโดยใช้วิธี CadRasterizationOptions.Layers.Add
  4. สร้างอินสแตนซ์ของ ImageOptionsBase และตั้งค่าคุณสมบัติ VectorRasterizationOptions เป็นอินสแตนซ์ของ CadRasterizationOptions ที่สร้างในขั้นตอนก่อนหน้า
  5. เรียกใช้วิธี Image.Save โดยส่งเส้นทางไฟล์ (หรือวัตถุ MemoryStream) รวมถึงอินสแตนซ์ของ ImageOptionsBase ที่สร้างในขั้นตอนก่อนหน้า

นี่คือโค้ดต้นฉบับทั้งหมด

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of Image
using (var image = Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers= new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
MyDir = MyDir + "CADLayersToRasterImageFormats_out.jpg";
image.Save(MyDir, options);
}
Console.WriteLine("\nCAD layers converted successfully to raster image format.\nFile saved at " + MyDir);
}
public static void ConvertAllLayersToRasterImageFormats()
{
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers = new string[] { "LayerA" };
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}
Console.WriteLine("\nCAD all layers converted successfully to raster image format.");

แปลงเลเยอร์ CAD ทั้งหมดเป็นรูปภาพแยกต่างหาก

คุณสามารถรับเลเยอร์ทั้งหมดจากการวาด CAD โดยใช้ CadImage.Layers และเรนเดอร์แต่ละเลเยอร์เป็นรูปภาพแยกต่างหากตามที่แสดงด้านล่าง

// For complete examples and data files, please go to https://github.com/aspose-cad/Aspose.CAD-for-.NET
// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string sourceFilePath = MyDir + "conic_pyramid.dxf";
// Load a CAD drawing in an instance of CadImage
using (var image = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
// Create an instance of CadRasterizationOptions
var rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
// Set image width & height
rasterizationOptions.PageWidth = 500;
rasterizationOptions.PageHeight = 500;
// Set the drawing to render at the center of image
rasterizationOptions.CenterDrawing = true;
// Get the layers in an instance of CadLayersDictionary
var layersList = image.Layers;
// Iterate over the layers
foreach (var layerName in layersList.GetLayersNames())
{
// Display layer name for tracking
Console.WriteLine("Start with " + layerName);
// Add the layer name to the CadRasterizationOptions's layer list
rasterizationOptions.Layers.Add(layerName);
// Create an instance of JpegOptions (or any ImageOptions for raster formats)
var options = new Aspose.CAD.ImageOptions.JpegOptions();
// Set VectorRasterizationOptions property to the instance of CadRasterizationOptions
options.VectorRasterizationOptions = rasterizationOptions;
//Export each layer to Jpeg format
image.Save(layerName + "_out.jpg", options);
}
}

การแปลงเลเยอร์ CAD DWF เป็นรูปภาพเรเซอร์รูปแบบ

Aspose.CAD สำหรับ .NET API ได้มีวิธีการที่มีประสิทธิภาพและใช้งานง่ายเพื่อระบุชื่อของเลเยอร์ CAD ที่ต้องการและเรนเดอร์มันเป็นรูปภาพเรเซอร์ ในที่นี้คือการทำใน 5 ขั้นตอนง่ายๆ ดังที่ระบุไว้ด้านล่างนี้

  1. โหลดไฟล์ CAD DWF ลงในอินสแตนซ์ของ Image โดยใช้วิธี Load
  2. สร้างอินสแตนซ์ของ CadRasterizationOptions และตั้งค่าคุณสมบัติที่จำเป็น เช่น PageWidth & PageHeight
  3. เพิ่มชื่อเลเยอร์ที่ต้องการโดยใช้วิธี CadRasterizationOptions.Layers.Add
  4. สร้างอินสแตนซ์ของ ImageOptionsBase และตั้งค่าคุณสมบัติ VectorRasterizationOptions เป็นอินสแตนซ์ของ CadRasterizationOptions ที่สร้างในขั้นตอนก่อนหน้า
  5. เรียกใช้วิธี Image.Save โดยส่งเส้นทางไฟล์ (หรือวัตถุ MemoryStream) รวมถึงอินสแตนซ์ของ ImageOptionsBase ที่สร้างในขั้นตอนก่อนหน้า

นี่คือโค้ดต้นฉบับทั้งหมด

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_ConvertingCAD();
string inputFile = MyDir + "18-12-11 9644 - site.dwf";
using (Image image = Image.Load(inputFile))
{
BmpOptions bmpOptions = new BmpOptions();
var dwfRasterizationOptions = new CadRasterizationOptions();
bmpOptions.VectorRasterizationOptions = dwfRasterizationOptions;
dwfRasterizationOptions.PageHeight = 500;
dwfRasterizationOptions.PageWidth = 500;
// export
string outPath = MyDir + "18-12-11 9644 - site.bmp";
image.Save(outPath, bmpOptions);
}

Aspose.CAD สำหรับ .NET จะเขียนข้อมูลเกี่ยวกับ API และหมายเลขเวอร์ชันในเอกสารผลลัพธ์โดยตรง ตัวอย่างเช่น ขณะเรนเดอร์เอกสารเป็น PDF Aspose.CAD สำหรับ .NET จะเติมข้อมูลในฟิลด์ Application ด้วยค่า ‘Aspose.CAD’ และฟิลด์ PDF Producer ด้วยค่า เช่น ‘Aspose.CAD v 17.10’

กรุณาทราบว่าคุณไม่สามารถสั่งให้ Aspose.CAD สำหรับ .NET เปลี่ยนแปลงหรือเอาข้อมูลนี้ออกจากเอกสารผลลัพธ์ได้