原創(chuàng)|使用教程|編輯:黃竹雯|2019-04-30 14:06:55.000|閱讀 416 次
概述:CAD .NET是一款在CAD領(lǐng)域被廣泛應(yīng)用的控件,可以快速準(zhǔn)確的閱讀DWG和DXF文件,并且通過(guò)Windows GDI+方法繪制件,支持多種文件格式,包括DWG、DXF、Gerber、光柵圖像等,并支持部分編輯功能。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
CAD .NET是一款在CAD領(lǐng)域被廣泛應(yīng)用的控件,可以快速準(zhǔn)確的閱讀DWG和DXF文件,并且通過(guò)Windows GDI+方法繪制件,支持多種文件格式,包括DWG、DXF、Gerber、光柵圖像等,并支持部分編輯功能。
CAD .NET應(yīng)用領(lǐng)域:
問(wèn):我正在尋找可以選擇圖像的一部分的東西,就像選擇一個(gè)部分來(lái)縮放那個(gè)部分,然后打印或?qū)С瞿莻€(gè)可見(jiàn)的部分?
答:您可以使用重載方法CADImage.SaveToStream的以下簽名將CAD圖像的一部分保存到MemoryStream:
public virtual void SaveToStream( Stream str, ImageFormat ImgFormat, DRect aCurRect, Rectangle clipRect )
ImgFormat參數(shù)指定保存圖像的文件格式(Bmp,Jpeg等)。CurRect參數(shù)表示當(dāng)前顯示在屏幕上的CAD圖像部分,而clipRect確定將保存到流的部分(Stream str)。
將裁剪的部分放入內(nèi)存流后,可以從中創(chuàng)建新的位圖:
MemoryStream ms = new MemoryStream(); ... Bitmap bmp = new Bitmap(ms);
然后使用PrintDocument類在打印機(jī)頁(yè)面上繪制此位圖:
public static void PrintBitmap(Bitmap bitmap, string printerName, int paperWidth, int paperHeight) { PrintDocument pd = new PrintDocument(); pd.PrinterSettings.PrinterName = printerName; pd.PrinterSettings.DefaultPageSettings.Landscape = true; pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom size", paperWidth, paperHeight); pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); pd.PrintPage += (sender, args) => { Rectangle m = args.MarginBounds; if ((double)bitmap.Width / (double)bitmap.Height > (double)m.Width / (double)m.Height) { m.Height = (int)((double)bitmap.Height / (double)bitmap.Width * (double)m.Width); } else { m.Width = (int)((double)bitmap.Width / (double)bitmap.Height * (double)m.Height); } args.Graphics.DrawImage(bitmap, m); }; pd.Print(); }
要使用鼠標(biāo)選擇CAD圖像的一部分,可以使用CADEditorControl.ClipRectangle工具,如下面的代碼示例所示。
using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; using System.Windows.Forms; using CADImport; using CADImport.FaceModule; public partial class Form1 : Form { public Form1() { InitializeComponent(); cadEditorControl1.EditorCADPictureBox.MouseDown += EditorCADPictureBox_MouseDown; cadEditorControl1.EditorCADPictureBox.MouseUp += EditorCADPictureBox_MouseUp; } void EditorCADPictureBox_MouseUp(object sender, MouseEventArgs e) { if (cadEditorControl1.ClipRectangle.Enabled) { MemoryStream ms = new MemoryStream(); DRect curRect = new DRect(cadEditorControl1.ImageRectangleF.Left, cadEditorControl1.ImageRectangleF.Top, 0, cadEditorControl1.ImageRectangleF.Right, cadEditorControl1.ImageRectangleF.Bottom, 0); cadEditorControl1.Image.SaveToStream(ms, ImageFormat.Bmp, curRect, cadEditorControl1.ClipRectangle.ClientRectangle); Bitmap bmp = new Bitmap(ms); PrintBitmap(bmp, "Microsoft Print to PDF", 297, 210); cadEditorControl1.ClipRectangle.DisableRect(); cadEditorControl1.Image.SelectionMode = SelectionEntityMode.Enabled; } } void EditorCADPictureBox_MouseDown(object sender, MouseEventArgs e) { cadEditorControl1.ClipRectangle.EnableRect(RectangleType.Zooming); cadEditorControl1.Image.SelectionMode = SelectionEntityMode.Disabled; }
問(wèn):需要確定的是:是否沒(méi)有方法來(lái)裁剪或使用SaveAsDXF在選定區(qū)域的DXF中獲得完整詳細(xì)的導(dǎo)出?
答:當(dāng)涉及到DXF導(dǎo)出時(shí),您不能保存選定的區(qū)域,您可以保存某些CAD實(shí)體(例如CADImage.SelectedEntities):
CADImage cadImage = new CADImage(); cadImage.InitialNewImage();
PS:放大時(shí),光柵圖像會(huì)丟失細(xì)節(jié)(變得模糊和像素化)
問(wèn):當(dāng)加載一個(gè)dwg,它有一個(gè)文本層,但是根據(jù)字體類型,編輯器沒(méi)有顯示它說(shuō)什么,而是顯示了很多字符。加載文件時(shí),有一些方法可以更改這些值的源。
答:文本字符是從存儲(chǔ)文本樣式使用的字體的字體文件(.shx .ttf)中讀取的。似乎給定的文本需要一些SHX字體,但缺少所需的字體或您的程序根本不使用SHX字體。您能否嘗試通過(guò)CADText.Style.FontName(單行文本)或CADMText.Style.FontName(多行文本)屬性確定所需字體的名稱?
問(wèn):我想更改塊內(nèi)一條線的終點(diǎn)。所以我為該線分配了一個(gè)新的終點(diǎn),但是這些變化是不可見(jiàn)的。根據(jù)線的長(zhǎng)度等屬性似乎很好。如何更改塊內(nèi)線的終點(diǎn)?
答:在塊中更改某個(gè)實(shí)體后,需要為該實(shí)體和CADBlock對(duì)象調(diào)用CADImage.Converter.Loads()方法。例如:
cadImage.Converter.Loads(cadLine); cadImage.Converter.Loads(cadBlock);
當(dāng)塊被插入到圖紙作為INSERT實(shí)體,你需要調(diào)用CADImage.Converter.Loads()也為CADInsert對(duì)象并調(diào)用CADImage.GetExtents()方法來(lái)重新計(jì)算該圖的范圍。
如果更改后發(fā)現(xiàn)實(shí)體長(zhǎng)度改變,顯示了新的長(zhǎng)度,但選擇卻是錯(cuò)誤的,我們可以嘗試使用以下代碼更新insert中的行:
cadImage.Converter.Loads(Line) cadImage.Converter.Loads(block) cadImage.SetNewPosEntity(0, 0, 0, insert)
問(wèn):在將表單v11更新為v12后,我仍然面臨幾個(gè)問(wèn)題,其中大多數(shù)與選擇有關(guān)。我正在使用SelectExt()函數(shù),該函數(shù)應(yīng)該在給定點(diǎn)返回所選實(shí)體,此函數(shù)不返回實(shí)體。設(shè)置CADSelector.UseShiftToAddSelected = True將返回實(shí)體,但也將允許多次選擇,這是不需要的。使用Select()而不是SelectExt()返回true,兩個(gè)函數(shù)不應(yīng)該相同嗎?
有一個(gè)名為clearPrevSelected(bool)的參數(shù),在v11中:將值設(shè)置為true將取消選擇其他實(shí)體并選擇新實(shí)體;在v12中:將值設(shè)置為true將不會(huì)取消選擇除選擇已選擇的實(shí)體之外的任何實(shí)體。如果要選擇未選擇的實(shí)體,則此函數(shù)將返回null。為什么param仍然被稱為clearPrevSelected,但不會(huì)像以前的版本那樣?此參數(shù)現(xiàn)在確定是否要選擇或取消選擇實(shí)體,而不是取消選擇其他實(shí)體。
答:在v12中,CADSelector.SelectExt()方法行為取決于所述的CADSelector.UseShiftToAddSelected屬性值。方法的第三個(gè)參數(shù)(clearPrevSelection)實(shí)際上采用Shift鍵狀態(tài)(按下并保持或未按下)。 如果不需要多項(xiàng)選擇,你應(yīng)該執(zhí)行以下操作:
Me.cadImage.SelectExt(e.X, e.Y, False, True)
要通過(guò)一次調(diào)用清除SelectedEntities和Markers集合,請(qǐng)使用CADImage.Selector.UndoSelect()方法。
問(wèn):在CADBlock中添加了幾個(gè)CADPolylines,最后一個(gè)我將它添加到CADInsert中,我控制了CADEditorControl.Image,但是,當(dāng)我想以DXF格式保存存儲(chǔ)在CADInsert中的元素時(shí),它不存儲(chǔ)元素也不是CADInsert。
代碼如下:
private bool PlaceEntity(CADEntity aEntity) { return PlaceEntity(aEntity, ""); } private bool PlaceEntity(CADEntity aEntity, string aLayoutName) { CADLayout vLayout; if (aLayoutName == "") vLayout = editor.Image.Layouts[0]; else vLayout = editor.Image.Converter.LayoutByName(aLayoutName); if (vLayout == null) return false; editor.Image.Converter.Loads(aEntity); vLayout.AddEntity(aEntity); return true; } private void DrawDoriArea(DPoint point) { CADBlock block = new CADBlock(); block.Name = "blockDoriArea"; block.AddEntity(DrawCamera(point)); block.AddEntity(DrawLens(point)); block.AddEntity(DrawIdentificationArea(point)); block.AddEntity(DrawRecognitionArea(point)); block.AddEntity(DrawObservationArea(point)); block.AddEntity(DrawDetectionArea(point)); block.AddEntity(DrawArc(point)); CADInsert insert = new CADInsert(); insert.Block = block; if (!PlaceEntity(insert)) editor.Image.Converter.GetSection(ConvSection.Blocks).RemoveEntityByName("blockDoriArea"); }
答:元素(給定案例中的CADPolylines)實(shí)際存儲(chǔ)在CADBlock中,而不是CADInsert中。CADInsert只是通過(guò)CADInsert.Block屬性引用CADBlock。
上述代碼中存在兩個(gè)問(wèn)題:
private void AddEntToSection(ConvSection aSection, CADEntity aEntity) { editor.Image.Converter.Loads(aEntity); editor.Image.Converter.GetSection(aSection).AddEntity(aEntity); } ...<strong> CADBlock block = new CADBlock(); block.Name = "blockDoriArea"; AddEntToSection(ConvSection.Blocks, block);
insert.Point = new DPoint(0, 0, 0);
PS:你可以使用任何(X,Y,Z)值,具體取決于你要放置CADInsert對(duì)象的位置。(0,0,0)只是舉的一個(gè)例子。
問(wèn):從v11升級(jí)到v12后,我使用Selector.MultipleSelect時(shí)出現(xiàn)了另一個(gè)問(wèn)題。在v12中,Selector類總是返回一個(gè)空集合。你可以調(diào)查一下是否可以按預(yù)期工作嗎?
答:在v12中,CADSelector.MultipleSelect()方法的行為取決于CADSelector.UseShiftToAddSelected屬性值,該值確定了一種可用的選擇模式:
CADSelector.UseShiftToAddSelected = False(默認(rèn)情況下) - 允許每個(gè)選定的對(duì)象(對(duì)象組)被添加到當(dāng)前選擇集而不丟棄先前的選擇。你必須按住Shift鍵并使用鼠標(biāo)左鍵放棄先前的選擇。
CADSelector.UseShiftToAddSelected = True - 在選擇一個(gè)或多個(gè)項(xiàng)目后嘗試在圖形中選擇更多對(duì)象時(shí),先前選擇的對(duì)象將變?yōu)槲催x中狀態(tài)。你必須按住Shift鍵并使用鼠標(biāo)左鍵才能將新對(duì)象添加到選擇集。
上述選擇模式的工作方式與使用Shift添加到 AutoCAD中的選擇選項(xiàng)的方式相同:
CADSelector.MultipleSelect()方法的第二個(gè)參數(shù)實(shí)際上采用Shift鍵狀態(tài)(按下并保持或未按下)。當(dāng)CADSelector.UseShiftToAddSelected = False并且此參數(shù)設(shè)置為True(模擬按住Shift鍵時(shí)的情況)時(shí),你只能放棄先前的選擇。這是符合預(yù)期的行為。
問(wèn):在演示之后實(shí)現(xiàn)了CAD Viewer。打開所有類型的圖像并應(yīng)用縮放。但是有兩個(gè)問(wèn)題:當(dāng)我打開圖像dwg或dxf時(shí),沒(méi)有顏色,我無(wú)法解釋被奇怪符號(hào)替換的字母。我忘記了什么?截圖如下:
代碼如下:
public void LoadFile(string fileName) { _fileName = fileName; if (fileName != null) { if (cadImage != null) { cadImage.Dispose(); cadImage = null; //this.lForm.LayerList.Clear(); } ImageScale = 1.0f; this.cadImage = CADImage.CreateImageByExtension(fileName); CADImport.CADConst.DefaultSHXParameters.UseSHXFonts = this.useSHXFonts; if (this.useSHXFonts) DoSHXFonts(); else DoTTFFonts(); this.cadImage.ChangeGraphicsMode(graphicsMode, renderMode); this.cadImage.GraphicsOutMode = graphicsMode; this.cadImage.ChangeDrawMode(graphicsMode, cadPictBox); this.cadImage.ChangeGraphicsMode(graphicsMode, renderMode); if (this.cadImage is CADRasterImage) (this.cadImage as CADRasterImage).Control = this.cadPictBox; } if (this.cadImage != null) { CADImage.CodePage = System.Text.Encoding.Default.CodePage;//note - charset was set here CADImage.LastLoadedFilePath = Path.GetDirectoryName(fileName); CreateNewLoadThread(fileName); } }
private void LoadCADImage(object fileNameObj) { lock (cadImage) { string fileName = (string)fileNameObj; if (CADConst.IsWebPath(fileName)) this.cadImage.LoadFromWeb(fileName); else cadImage.LoadFromFile(fileName); } SetCADImageOptions(); }
public void SetCADImageOptions() { cadImage.IsShowLineWeight = this.showLineWeight; cadImage.IsWithoutMargins = true; cadImage.UseDoubleBuffering = this.useDoubleBuffering; cadImage.TTFSmoothing = TTFSmoothing.None; this.useSelectEntity = false; if (cadPictBox.BackColor == Color.White) White_Click(); else Black_Click(); if (this.DrawMode == false) this.DoBlackColor(); ObjEntity.cadImage = cadImage; ObjEntity.propGrid = this.propGrid; DoResize(true, true); this.cadPictBox.Invalidate(); }
答:我注意到您的代碼包含DoBlackColor()調(diào)用,在ViewerDemo項(xiàng)目中,給定的方法呈現(xiàn)黑白CAD圖像。要確定當(dāng)前使用的渲染模式,您應(yīng)該檢查CADImage.DrawMode屬性值,該值可能如下所示:
public enum CADDrawMode { // All colors are shown. Normal = 0, // CAD image is shown in black and white. Black = 1, // CAD image is shown in grayscale. Gray = 2, }
錯(cuò)誤字符的問(wèn)題可能與使用不正確的字體有關(guān)。請(qǐng)檢查圖形文件需要哪些SHX和TTF字體(例如,在AutoCAD中)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn