新手入門必看:VectorDraw 常見問題整理大全(九)
VectorDraw Developer Framework(VDF)是一個用于應用程序可視化的圖形引擎庫。有了VDF提供的功能,您可以輕松地創(chuàng)建、編輯、管理、輸出、輸入和打印2D和3D圖形文件。該庫還支持許多矢量和柵格輸入和輸出格式,包括本地PDF和SVG導出。
【VectorDraw Developer Framework最新版下載】
VectorDraw web library (javascript)是一個矢量圖形庫。VectorDraw web library (javascript)不僅能打開CAD圖紙,而且能顯示任何支持HTML5標準平臺上的通用矢量對象,如Windows,安卓,iOS和Linux。無需任何安裝,VectorDraw web library (javascript)就可以運行在任何支持canvas標簽和Javascript的主流瀏覽器(Chrome, Firefox, Safari, Opera, Dolphin, Boat等等)中。
【VectorDraw web library (javascript)最新版下載】
一. 窗口選擇地面對象的三角形
問:如何通過窗口選擇vdGroundSurface對象來選擇/獲取某些三角形?
答:在新的C#項目中添加一個vdFramedControl并添加如下代碼:
private void Form1_Load(object sender, EventArgs e) { vdFramedControl.CommandLine.CommandExecute += new VectorDraw.Professional.vdCommandLine.CommandExecuteEventHandler(CommandLine_CommandExecute); } void CommandLine_CommandExecute(string commandname, bool isDefaultImplemented, ref bool success) { if (isDefaultImplemented) return; if (string.Compare(commandname, "test", true) == 0) { success = true; vdDocument document = vdFramedControl.BaseControl.ActiveDocument; Box rectInViewCS = null; document.Prompt("Pick a window select"); StatusCode sc = document.ActionUtility.getUserRectViewCS(null, out rectInViewCS); document.Prompt(null); if (sc != StatusCode.Success) return; gPoints pts = new gPoints(); pts.Add(rectInViewCS.Min); pts.Add(rectInViewCS.Max); vdSelection set = new vdSelection(); set.SetUnRegisterDocument(document); vdFilterObject filterselect = new vdFilterObject(); filterselect.Types.AddItem("vdGroundSurface"); set.Select(RenderSelect.SelectingMode.CrossingWindowRectangle,pts); set.ApplyFilter(filterselect); if (set.Count == 0) return; vdGroundSurface gsf = set[0] as vdGroundSurface; gTriangles triangles = gsf.Triangles; VectorDraw.Render.Region rgn = new VectorDraw.Render.Region(rectInViewCS); int trigid = 0; VectorDraw.Generics.vdArray trgiIds = new vdArray(); gPoint p1, p2, p3; foreach (gTriangle trig in triangles) { p1 = document.World2ViewMatrix.Transform(trig.P1); p2 = document.World2ViewMatrix.Transform(trig.P2); p3 = document.World2ViewMatrix.Transform(trig.P3); if (rgn.IsLineInside(p1, p2) || rgn.IsLineIntersect(p1, p2)) trgiIds.AddItem(trigid); trigid++; if (rgn.IsLineInside(p2, p3) || rgn.IsLineIntersect(p2, p3)) trgiIds.AddItem(trigid); trigid++; if (rgn.IsLineInside(p3, p1) || rgn.IsLineIntersect(p3, p1)) trgiIds.AddItem(trigid); trigid++; } //test geting triangles from trgiIds foreach (int var in trgiIds) { gTriangle trig = triangles[(var + 1) / 3]; int trigline = var % 3;//0 is p1,p2 - 1 is p1,p2 - 2 = p3,p1 } } }
您可以加載具有復雜Groundsurface對象的圖形,然后在命令提示符中寫入“test”以運行代碼。
二. 檢查點是否在折線內
問:如何檢查點是否在折線內?
答:可以創(chuàng)建具有這些功能的Region(對象)。請檢查以下示例代碼。
//Create A Polyline vdPolyline poly = new vdPolyline(); poly.SetUnRegisterDocument(vdFramedControl1.BaseControl.ActiveDocument); poly.setDocumentDefaults(); poly.VertexList.Add(new gPoint()); poly.VertexList.Add(new gPoint(1,1)); poly.VertexList.Add(new gPoint(2,2)); poly.VertexList.Add(new gPoint(3,2)); poly.VertexList.Add(new gPoint(5,2)); poly.VertexList.Add(new gPoint(6,1)); poly.VertexList.Add(new gPoint(3,0)); poly.VertexList.Add(new gPoint(0,0)); vdFramedControl1.BaseControl.ActiveDocument.Model.Entities.AddItem(poly); //Create A Region From the Polyline's points VectorDraw.Render.Region reg = new VectorDraw.Render.Region(poly.GetSamplePoints(0, 0)); //Check if a point is inside the polyline or not. bool a = reg.IsPointInside (new gPoint (0,1)); bool b = reg.IsPointInside (new gPoint (2,1));
三. 將實體從一個文檔復制到另一個文檔
問:如何將一些實體從一個控件復制到另一個控件?
答:使用VDF Prof. 6.x Wrapper或VDProControl(vdraw.ocx / vdprocontrol.dll),您可以嘗試以下代碼:
Private Sub copy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copy.Click Dim coloritem As VectorDraw.Professional.vdObjects.vdColor Dim cadfig As vdFigure Dim selset As vdSelection Dim docfrom As VectorDraw.Professional.vdObjects.vdDocument Dim docto As VectorDraw.Professional.vdObjects.vdDocument vdto.ActiveDocument.New() vdfrom.ActiveDocument.Open(Application.StartupPath() + "\PD-23-from.vdi", 0, 0) vdfrom.CommandAction.Zoom("e", 0, 0) ''create a temporary selection to add the entities you want to copy in destination document selset = vdfrom.ActiveDocument.Selections.Add("tmpCopy") selset.RemoveAll() For Each cadfig In vdfrom.ActiveDocument.Entities If cadfig.Visible = False Then Continue For selset.AddItem(cadfig) Next ''get the the VectorDraw FrameWork document objects. docfrom = vdfrom.ActiveDocument.WrapperObject docto = vdto.ActiveDocument.WrapperObject ''disable the undo write in destination document docto.UndoHistory.PushEnable(False) ''Use the MergeSelection to copy the entites and table dependecies from source to destination document docto.MergeSelection(selset.WrapperObject) ''Set the pallete and backround colors of destination same as the source document docto.Palette.Background = docfrom.Palette.Background docto.Palette.Forground = docfrom.Palette.Forground docto.Palette.RemoveAll() For Each coloritem In docfrom.Palette docto.Palette.AddItem(coloritem.Clone()) Next docto.Model.BkColorEx = docfrom.ActiveLayOut.BkColorEx ''pop back the undo mode of the destination to its original value. docto.UndoHistory.PopEnable() vdto.CommandAction.Zoom("e", 0, 0) vdto.ActiveDocument.SaveAs(Application.StartupPath() + "\PD-23-to.vdi", VdConstFileVer.DefaultVersion) End Sub
兩個VD控件是名稱vdfrom和vdto。
四. 使用ToStream方法將圖形作為存儲在數據庫中的塊插入
問:如何使用ToStream方法將圖形作為存儲在數據庫中的塊插入?
答:您可以使用vdDocument的ToStream方法在數據庫中保存/讀取圖形(文檔),以將繪圖保存在System.IO.MemoryStream和LoadFromMemory中以讀取它,如:
object ByteArray = null; System.IO.MemoryStream memorystream = Doc.ToStream(); //doc can be the BaseControl.ActiveDocument or vdDoc1.Document or vdFrame.BaseControl.ActiveDocument if (memorystream == null) return 0; ByteArray = memorystream.ToArray(); int size = (int)memorystream.Length; memorystream.Close(); // then SAVE the ByteArray to your database and // Read the Bytes from database and store it in the ByteArray and then : System.IO.MemoryStream memorystream = new System.IO.MemoryStream((byte[])ByteArray); memorystream.Position = 0; Doc.LoadFromMemory(memorystream); memorystream.Close();
您可以在新項目中嘗試上述代碼,看看它是否正常工作。您也可以使用壓縮,請參閱文章: http://www.vdraw.com/Export6/60000177.htm使用ToStream(true)和LoadFromMemory(stream,true)。
如果您希望以塊的形式插入已存儲在數據庫中的文檔,請嘗試以下操作:
創(chuàng)建一個新的C#2005項目,并在新表單中添加一個vdScrollable控件(名為vdSC_From),一個名為vdDocComp_Temp的vdDocument控件,一個名為vdFramedControl的控件vdFC_To和2個按鈕(button1和button2)。
該示例內部沒有DATABASE代碼,為此,vdScrollable控件(vdSC_from控件)用于在ByteArray中創(chuàng)建和保存文檔,因此它將用于模擬數據庫記錄/字段中ByteArray的填充。然后使用臨時vdDocument控件(vdDocComp_Temp)將此ByteArray“轉換”回Document,然后使用vdBlocks對象的AddFromDocument方法生成插入vdFramed控件的圖形(作為vdInsert)的vdBlock對象。另見下文,代碼應該是:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Instead of a database, a VectorDraw Scrollable control (named vdSC_From) is used to create a drawing to a bytearray //Also a vdDocument component (named vdDocComp_Temp) is used so it can be filled with the drawing that is stored in the database (named DocumentToBlock) //after that in the Target's Document (the vdFC_To control) a block is created with AddFromDocument and inserted private void button1_Click(object sender, EventArgs e) { //instead of a database, a VectorDraw Scrollable control (vdSC_From) is used to create a drawing to a bytearray vdSC_From.BaseControl.ActiveDocument.New(); vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdCircle(new VectorDraw.Geometry.gPoint(0, 0, 0), (double)2.0); vdSC_From.BaseControl.ActiveDocument.ActivePenColor.ColorIndex = 1; vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdEllipse(new VectorDraw.Geometry.gPoint(0, 0, 0), new VectorDraw.Geometry.gPoint(1, 1, 0), (double)3.0); object ByteArray = null; System.IO.MemoryStream memorystream = vdSC_From.BaseControl.ActiveDocument.ToStream(true); //save to memory compressed if (memorystream == null) return ; ByteArray = memorystream.ToArray(); int size = (int)memorystream.Length; memorystream.Close(); // the drawing/document is created and saved to a public ByteArray // now we will read this to the temporary document VectorDraw.Professional.vdObjects.vdDocument DocumentToBlock = vdDocComp_Temp.Document; DocumentToBlock.New(); memorystream = new System.IO.MemoryStream((byte[])ByteArray); memorystream.Position = 0; DocumentToBlock.LoadFromMemory(memorystream, true);//Read from memory compressed memorystream.Close(); //and add the entities of this document to a block //We create a block object and initialize it's default properties. VectorDraw.Professional.vdCollections.vdBlocks blks = vdFC_To.BaseControl.ActiveDocument.Blocks; VectorDraw.Professional.vdPrimaries.vdBlock blk = blks.AddFromDocument("CustomBlock1",DocumentToBlock,true); blk.Update(); //add the block to the blocks collection vdFC_To.BaseControl.ActiveDocument.Blocks.AddItem(blk); //and now insert it; vdFC_To.BaseControl.ActiveDocument.CommandAction.CmdInsert("CustomBlock1", new VectorDraw.Geometry.gPoint(0.0d, 3.0d, 0.0d), 1d, 2d, 2d); } private void button2_Click(object sender, EventArgs e) { //instead of a database, a VectorDraw Scrollable control (vdSC_From) is used to create a drawing to a bytearray vdSC_From.BaseControl.ActiveDocument.New(); vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdRect(new VectorDraw.Geometry.gPoint(2.0d, 1d, 0d), new VectorDraw.Geometry.gPoint(3.0d, 4d, 0d)); vdSC_From.BaseControl.ActiveDocument.ActivePenColor.ColorIndex = 3; vdSC_From.BaseControl.ActiveDocument.CommandAction.CmdArc(new VectorDraw.Geometry.gPoint(4.0d, 0d, 0d),2d, 1.0d,2.0d); object ByteArray = null; System.IO.MemoryStream memorystream = vdSC_From.BaseControl.ActiveDocument.ToStream(true); if (memorystream == null) return; ByteArray = memorystream.ToArray(); int size = (int)memorystream.Length; memorystream.Close(); // the drawing/document is created and saved to a public ByteArray // now we will read this to the temporary document VectorDraw.Professional.vdObjects.vdDocument DocumentToBlock = vdDocComp_Temp.Document; DocumentToBlock.New(); memorystream = new System.IO.MemoryStream((byte[])ByteArray); memorystream.Position = 0; DocumentToBlock.LoadFromMemory(memorystream,true); memorystream.Close(); //and add the entities of this document to a block //We create a block object and initialize it's default properties. VectorDraw.Professional.vdCollections.vdBlocks blks = vdFC_To.BaseControl.ActiveDocument.Blocks; VectorDraw.Professional.vdPrimaries.vdBlock blk = blks.AddFromDocument("CustomBlock2", DocumentToBlock, true); blk.Update(); //add the block to the blocks collection vdFC_To.BaseControl.ActiveDocument.Blocks.AddItem(blk); //and now insert it; vdFC_To.BaseControl.ActiveDocument.CommandAction.CmdInsert("CustomBlock2", new VectorDraw.Geometry.gPoint(0.0d, 3.0d, 0.0d), 1d, 2d, 2d); } }
未完待續(xù)......