原創|使用教程|編輯:黃竹雯|2019-04-30 14:06:55.000|閱讀 416 次
概述:CAD .NET是一款在CAD領域被廣泛應用的控件,可以快速準確的閱讀DWG和DXF文件,并且通過Windows GDI+方法繪制件,支持多種文件格式,包括DWG、DXF、Gerber、光柵圖像等,并支持部分編輯功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
CAD .NET是一款在CAD領域被廣泛應用的控件,可以快速準確的閱讀DWG和DXF文件,并且通過Windows GDI+方法繪制件,支持多種文件格式,包括DWG、DXF、Gerber、光柵圖像等,并支持部分編輯功能。
CAD .NET應用領域:
問:我正在尋找可以選擇圖像的一部分的東西,就像選擇一個部分來縮放那個部分,然后打印或導出那個可見的部分?
答:您可以使用重載方法CADImage.SaveToStream的以下簽名將CAD圖像的一部分保存到MemoryStream:
public virtual void SaveToStream( Stream str, ImageFormat ImgFormat, DRect aCurRect, Rectangle clipRect )
ImgFormat參數指定保存圖像的文件格式(Bmp,Jpeg等)。CurRect參數表示當前顯示在屏幕上的CAD圖像部分,而clipRect確定將保存到流的部分(Stream str)。
將裁剪的部分放入內存流后,可以從中創建新的位圖:
MemoryStream ms = new MemoryStream(); ... Bitmap bmp = new Bitmap(ms);
然后使用PrintDocument類在打印機頁面上繪制此位圖:
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();    
}
要使用鼠標選擇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;
    }
問:需要確定的是:是否沒有方法來裁剪或使用SaveAsDXF在選定區域的DXF中獲得完整詳細的導出?
答:當涉及到DXF導出時,您不能保存選定的區域,您可以保存某些CAD實體(例如CADImage.SelectedEntities):
CADImage cadImage = new CADImage(); cadImage.InitialNewImage();
PS:放大時,光柵圖像會丟失細節(變得模糊和像素化)
問:當加載一個dwg,它有一個文本層,但是根據字體類型,編輯器沒有顯示它說什么,而是顯示了很多字符。加載文件時,有一些方法可以更改這些值的源。

答:文本字符是從存儲文本樣式使用的字體的字體文件(.shx .ttf)中讀取的。似乎給定的文本需要一些SHX字體,但缺少所需的字體或您的程序根本不使用SHX字體。您能否嘗試通過CADText.Style.FontName(單行文本)或CADMText.Style.FontName(多行文本)屬性確定所需字體的名稱?
問:我想更改塊內一條線的終點。所以我為該線分配了一個新的終點,但是這些變化是不可見的。根據線的長度等屬性似乎很好。如何更改塊內線的終點?
答:在塊中更改某個實體后,需要為該實體和CADBlock對象調用CADImage.Converter.Loads()方法。例如:
cadImage.Converter.Loads(cadLine); cadImage.Converter.Loads(cadBlock);
當塊被插入到圖紙作為INSERT實體,你需要調用CADImage.Converter.Loads()也為CADInsert對象并調用CADImage.GetExtents()方法來重新計算該圖的范圍。
如果更改后發現實體長度改變,顯示了新的長度,但選擇卻是錯誤的,我們可以嘗試使用以下代碼更新insert中的行:
cadImage.Converter.Loads(Line) 
        cadImage.Converter.Loads(block) 
        cadImage.SetNewPosEntity(0, 0, 0, insert)
問:在將表單v11更新為v12后,我仍然面臨幾個問題,其中大多數與選擇有關。我正在使用SelectExt()函數,該函數應該在給定點返回所選實體,此函數不返回實體。設置CADSelector.UseShiftToAddSelected = True將返回實體,但也將允許多次選擇,這是不需要的。使用Select()而不是SelectExt()返回true,兩個函數不應該相同嗎?
有一個名為clearPrevSelected(bool)的參數,在v11中:將值設置為true將取消選擇其他實體并選擇新實體;在v12中:將值設置為true將不會取消選擇除選擇已選擇的實體之外的任何實體。如果要選擇未選擇的實體,則此函數將返回null。為什么param仍然被稱為clearPrevSelected,但不會像以前的版本那樣?此參數現在確定是否要選擇或取消選擇實體,而不是取消選擇其他實體。
答:在v12中,CADSelector.SelectExt()方法行為取決于所述的CADSelector.UseShiftToAddSelected屬性值。方法的第三個參數(clearPrevSelection)實際上采用Shift鍵狀態(按下并保持或未按下)。 如果不需要多項選擇,你應該執行以下操作:
Me.cadImage.SelectExt(e.X, e.Y, False, True)
要通過一次調用清除SelectedEntities和Markers集合,請使用CADImage.Selector.UndoSelect()方法。
問:在CADBlock中添加了幾個CADPolylines,最后一個我將它添加到CADInsert中,我控制了CADEditorControl.Image,但是,當我想以DXF格式保存存儲在CADInsert中的元素時,它不存儲元素也不是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)實際存儲在CADBlock中,而不是CADInsert中。CADInsert只是通過CADInsert.Block屬性引用CADBlock。
上述代碼中存在兩個問題:
  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對象的位置。(0,0,0)只是舉的一個例子。
問:從v11升級到v12后,我使用Selector.MultipleSelect時出現了另一個問題。在v12中,Selector類總是返回一個空集合。你可以調查一下是否可以按預期工作嗎?
答:在v12中,CADSelector.MultipleSelect()方法的行為取決于CADSelector.UseShiftToAddSelected屬性值,該值確定了一種可用的選擇模式:
CADSelector.UseShiftToAddSelected = False(默認情況下) - 允許每個選定的對象(對象組)被添加到當前選擇集而不丟棄先前的選擇。你必須按住Shift鍵并使用鼠標左鍵放棄先前的選擇。
CADSelector.UseShiftToAddSelected = True - 在選擇一個或多個項目后嘗試在圖形中選擇更多對象時,先前選擇的對象將變為未選中狀態。你必須按住Shift鍵并使用鼠標左鍵才能將新對象添加到選擇集。
上述選擇模式的工作方式與使用Shift添加到 AutoCAD中的選擇選項的方式相同:

CADSelector.MultipleSelect()方法的第二個參數實際上采用Shift鍵狀態(按下并保持或未按下)。當CADSelector.UseShiftToAddSelected = False并且此參數設置為True(模擬按住Shift鍵時的情況)時,你只能放棄先前的選擇。這是符合預期的行為。
問:在演示之后實現了CAD Viewer。打開所有類型的圖像并應用縮放。但是有兩個問題:當我打開圖像dwg或dxf時,沒有顏色,我無法解釋被奇怪符號替換的字母。我忘記了什么?截圖如下:

代碼如下:
 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()調用,在ViewerDemo項目中,給定的方法呈現黑白CAD圖像。要確定當前使用的渲染模式,您應該檢查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,
    } 
錯誤字符的問題可能與使用不正確的字體有關。請檢查圖形文件需要哪些SHX和TTF字體(例如,在AutoCAD中)。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn