新手入门必看:VectorDraw 常见问题整理大全(十八)
VectorDraw Developer Framework(VDF)是一个用于应用程序可视化的图形引擎库。有了VDF提供的功能,您可以轻松地创建、编辑、管理、输出、输入和打印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)最新版下载】
一. 按图层更改实体的顺序
问:我想更改某些实体的顺序,以便在2D中绘制其他实体。例如,层“TEXT”和“DIMS”中的vdTexts和vdDimensions对象我想将它们设置为实体列表的后面,以便它们在其他实体之上绘制。我尝试在for ...循环中使用ChangeOrder方法,但速度非常慢。我怎么才能更快地做到这一点?
答:绘制对象的顺序定义如下:
- 在2D中:添加的最后一个对象(在实体列表中,而不是在图层列表中)绘制在上一个对象的顶部
- 在3D中:对象的坐标指定哪个将在顶部。
因此,在2D中,你必须要更改这些实体的顺序才行。你可以使用InsertAt / RemoveAt和ChangeOrder方法或交换方法来交换集合中两个对象的位置。在Wrapper中,你可以使用ChangeOrder / MoveBefore / MoveAfter Subs。
如果你要检查很多图层和实体,而不是使用ChangeOrder或Swap,那么你可以尝试以下代码:
vdArray<vdLayer> layers = new vdArray<vdLayer>(); layers.AddItem(doc.Layers.FindName("1")); // here you add the layers with the order you want layers.AddItem(doc.Layers.FindName("DIMS")); // the order of the layers here is significant layers.AddItem(doc.Layers.FindName("TEXTS")); // and defines the order that the entities will have and drawn in 2D // if the drawing has the layers 0, 1, 2, 3, TEXTS, 4, DIMS, 5 then then entities draw order in 2D after // this code will be [entities in any layers 0,2,3,4,5][entities in Layer 1][entities in layer DIMS][entities in Layer TEXTS] layers.MakeIndexDictionary(); // speeds up swaping and search doc.Model.Entities.MakeIndexDictionary(); // speeds up swaping and search int pos1 = 0; int pos2 = 0; for (int i = 0; i < doc.Model.Entities.Count; i++) { vdFigure fig1 = doc.Model.Entities[i]; pos1 = layers.GetObjectRealPosition(fig1.Layer); for (int k = i + 1; k < doc.Model.Entities.Count; k++) { vdFigure fig2 = doc.Model.Entities[k]; pos2 = layers.GetObjectRealPosition(fig2.Layer); if (pos1 > pos2) { doc.Model.Entities.swap(fig1, fig2); } } } doc.Model.Entities.ClearIndexDictionary(); // must follow a doc.Model.Entities.MakeIndexDictionary layers.ClearIndexDictionary(); // must follow a layers.MakeIndexDictionary
或者,你可以使用排序类并对数组中的所有实体(System.Collections.ArrayList)进行排序,并将这些实体(已排序)重新添加回绘图,如:
internal class ItemSort : System.Collections.IComparer { vdFigure mfig; int mpos; ulong handle; public ulong Handle { get { return handle; } } public vdFigure Fig { get { return mfig; } } public ItemSort() { } public ItemSort(vdFigure fig, int pos) { mfig = fig; mpos = pos; handle = mfig.Handle.Value; // Keep the handle because the RemoveAll sets the handle to 0 !! } public int Compare(object x, object y) { return (((ItemShort)x).mpos - ((ItemShort)y).mpos); } } ..... vdArray<vdLayer> layers = new vdArray<vdLayer>(); layers.AddItem(doc.Layers.FindName("0")); // here you add the layers with the order you want layers.AddItem(doc.Layers.FindName("2")); // here you add the layers with the order you want layers.AddItem(doc.Layers.FindName("1")); // here you add the layers with the order you want layers.AddItem(doc.Layers.FindName("DIMS")); // the order of the layers here is significant layers.AddItem(doc.Layers.FindName("TEXTS")); // and defines the order that the entities will have and drawn in 2D // if the drawing has the layers 0, 1, TEXTS, 2 and DIMS then then entities draw order in 2D after // this code will be [entities in any layer 0][entities in any layer 2][entities in Layer 1][entities in layer DIMS][entities in Layer TEXTS] layers.MakeIndexDictionary(); // speeds up swaping and search System.Collections.ArrayList lst = new System.Collections.ArrayList(); foreach (vdFigure fig in doc.Model.Entities) { lst.Add(new ItemShort(fig, layers.GetObjectRealPosition(fig.Layer))); } layers.ClearIndexDictionary(); lst.Sort(new ItemSort()); // sort items doc.Model.Entities.RemoveAll(); //remove all entities from the model that have the "bad order" foreach (ItemShort item in lst) { item.Fig.Handle.InternalSetValue(item.Handle); // set the handle back cause it is lost with RemoveAll doc.Model.Entities.AddItem(item.Fig); //and re-add the entities with the order }
在版本6019Beta7及更高版本中,有一种新方法Sort()可用于代替上述代码段。详情如下:
在版本6019中,在vdArray对象和vdEntities集合中添加了新的排序方法。代码如下:
VectorDraw.Generics.vdArray<T> sort methods /// <summary> /// Sorts the elements in an entire collection using the <see cref="System.IComparable<T>"/> /// generic interface implementation of each element of the System.Array. /// </summary> /// <exception cref="System.InvalidOperationException">One or more elements in array do not implement the <see cref="System.IComparable<T>"/> generic interface.</exception> public void Sort() example: sort items in a collection of doubles vdArray<double> collectionOfDoubles = new vdArray<double>(); collectionOfDoubles.AddItem(5); collectionOfDoubles.AddItem(15); collectionOfDoubles.AddItem(2); collectionOfDoubles.AddItem(8); collectionOfDoubles.AddItem(3); //sort the items from smaller value to bigger collectionOfDoubles.Sort(); //the values will be take the following order //2 ,3, 5, 8, 15 /// <summary> /// Sorts the elements in a range of elements in entire collection using the specified /// <see cref="System.Collections.Generic.IComparer<T>"/> generic interface. /// </summary> /// <param name="comparer"> /// The <see cref="System.Collections.Generic.IComparer<T>"/> generic interface implementation /// to use when comparing elements, or null to use the <see cref="System.IComparable<T>"/> /// generic interface implementation of each element. /// </param> /// <exception cref="System.ArgumentException"> /// The implementation of comparer caused an error during the sort. /// For example, comparer might not return 0 when comparing an item with itself. /// </exception> /// <exception cref="System.InvalidOperationException"> /// comparer is null, and one or more elements in array do not implement the <see cref="System.IComparable<T>"/> generic interface /// </exception> public void Sort(IComparer<T> comparer) example: sort items in a collection of gPoints depend of their distance from an origin point. private class OriginComparer : System.Collections.Generic.IComparer<gPoint> { gPoint mOrigin; public OriginComparer(gPoint Origin) { mOrigin = Origin; } public int Compare(gPoint x, gPoint y) { double dif = x.Distance3D(mOrigin) - y.Distance3D(mOrigin); if (dif == 0.0d) return 0; if (dif > 0.0d) return 1; else return -1; } } vdArray<gPoint> collectionOfPoints = new vdArray<gPoint>(); collectionOfPoints.AddItem(new gPoint(5,0,0)); collectionOfPoints.AddItem(new gPoint(15, 0, 0)); collectionOfPoints.AddItem(new gPoint(2, 0, 0)); collectionOfPoints.AddItem(new gPoint(8, 0, 0)); collectionOfPoints.AddItem(new gPoint(3, 0, 0)); //sort the items from smaller to bigger depend of their distance from point(0,0,0) collectionOfPoints.Sort(new OriginComparer(new gPoint(0, 0, 0))); //the values will be take the following order //(2,0,0) ,(3,0,0), (5,0,0), (8,0,0), (15,0,0) VectorDraw.Professional.vdCollections.vdEntities sort methods /// <summary> /// Sorts the elements in a range of elements in entire collection using the specified /// <see cref="System.Collections.Generic.IComparer<vdFigure>"/> generic interface. /// </summary> /// <param name="comparer"> /// The <see cref="System.Collections.Generic.IComparer<vdFigure>"/> generic interface implementation /// to use when comparing elements, or null to use the <see cref="System.IComparable<vdFigure>"/> /// generic interface implementation of each element. /// </param> /// <exception cref="System.ArgumentException"> /// The implementation of comparer caused an error during the sort. /// For example, comparer might not return 0 when comparing an item with itself. /// </exception> /// <exception cref="System.InvalidOperationException"> /// comparer is null, and one or more elements in array do not implement the <see cref="System.IComparable<vdFigure>"/> generic interface /// </exception> public void Sort(IComparer<vdFigure> comparer) example: sort items in vdEntities collection depend of their Layer name. //a class object that used to compare to vdFigure depend of the Layer Name private class FigureLayerComparer : System.Collections.Generic.IComparer<vdFigure> { public FigureLayerComparer() { } public int Compare(vdFigure fig1, vdFigure fig2) { return System.StringComparer.InvariantCultureIgnoreCase.Compare(fig1.Layer.ToString(), fig2.Layer.ToString()); } } //Create 4 layers and add them into the Layers collection of a vdDocument object vdLayer lay1 = doc.Layers.Add("1"); vdLayer lay2 = doc.Layers.Add("2"); vdLayer lay3 = doc.Layers.Add("3"); vdLayer lay4 = doc.Layers.Add("4"); //create a new vdLine object that belong to Layer with name "3" vdLine l1 = new vdLine(); l1.SetUnRegisterDocument(doc); l1.setDocumentDefaults(); l1.Layer = lay3; //create a new vdLine object that belong to Layer with name "1" vdLine l2 = new vdLine(); l2.SetUnRegisterDocument(doc); l2.setDocumentDefaults(); l2.Layer = lay1; //create a new vdLine object that belong to Layer with name "4" vdLine l3 = new vdLine(); l3.SetUnRegisterDocument(doc); l3.setDocumentDefaults(); l3.Layer = lay4; //create a new vdLine object that belong to Layer with name "2" vdLine l4 = new vdLine(); l4.SetUnRegisterDocument(doc); l4.setDocumentDefaults(); l4.Layer = lay2; //add the new vdlines to vdDocument Model Entities collection doc.Model.Entities.AddItem(l1); doc.Model.Entities.AddItem(l2); doc.Model.Entities.AddItem(l3); doc.Model.Entities.AddItem(l4); //change the order of items in the collection so the items of layer "1" to be first in the collection folow by items in layer "2" , "3" and "4" at the end of the collection doc.Model.Entities.Sort(new FigureLayerComparer());
二. 制作复制命令以执行所选项目的多个副本
问:如何制作复制命令以执行所选项目的多个副本。例如,复制完选择后,复制命令不会结束,并且保持活动状态以继续复制相同的选择活动?
答:你可以通过创建自己的命令来完成此操作。在这种情况下,请尝试以下代码:
private void button2_Click(object sender, EventArgs e) { bool flag = MultipleCopy(vdFramedControl1.BaseControl.ActiveDocument); this.Text = flag.ToString(); } private Boolean MultipleCopy(vdDocument doc) { bool isCopy = true; doc.Prompt("Select Entities to copy: "); if (!doc.CommandAction.CmdSelect("USER")) { doc.Prompt(""); return false; } vdSelection sel = doc.Selections.FindName("VDRAW_PREVIOUS_SELSET"); if ((sel == null) | (sel.Count < 1)) return false; doc.Prompt("Copy from Point: "); gPoint ptFrom = doc.ActionUtility.getUserPoint() as gPoint; if ((ptFrom == null)) { doc.Prompt(""); return false; } int i = 0; while (isCopy) { isCopy = doc.CommandAction.CmdCopy(sel, ptFrom, "USER"); if ((i==0) & (isCopy==false)) { doc.Prompt(""); return false; } i++; } doc.Prompt(""); return true; }
未完待续~
热门活动
*点击图片查看活动详情*