public void SetVertexConstraints(IList <PFVertex> constr, GeometryBase cBase, double coef = 1.0, bool on = true) { foreach (var vrt in constr) { vrt.RestrictSupport = cBase; vrt.InfluenceCoef = coef; //vrt.SupportGuid = cBase.Id; } if (cBase.GetType() == typeof(Rhino.Geometry.Point)) { foreach (var vert in constr) { vert.RestrictPosition = vert.ConstrainPoint; } } else if (cBase.GetType() == typeof(Curve) || cBase.GetType() == typeof(NurbsCurve)) { foreach (var vert in constr) { vert.RestrictPosition = vert.ConstrainCurve; } } else if (cBase.GetType() == typeof(Brep)) { foreach (var vert in constr) { if (on) { vert.RestrictPosition = vert.ConstrainOnBrep; } else { vert.RestrictPosition = vert.ConstrainInBrep; } } } else if (cBase.GetType() == typeof(Mesh)) { foreach (var vert in constr) { if (on) { vert.RestrictPosition = vert.ConstrainOnMesh; } else { vert.RestrictPosition = vert.ConstrainInMesh; } } } }
internal static double DistanceToIntersection(Vertex centre, Vertex maxVertex, Edge e) { var centreProj = Vertex.ByCoordinates(centre.X, centre.Y, 0); var maxProj = Vertex.ByCoordinates(maxVertex.X, maxVertex.Y, 0); var startProj = Vertex.ByCoordinates(e.StartVertex.X, e.StartVertex.Y, 0); var endProj = Vertex.ByCoordinates(e.EndVertex.X, e.EndVertex.Y, 0); Edge rayEdge = Edge.ByStartVertexEndVertex(centreProj, maxProj); Edge edgeProj = Edge.ByStartVertexEndVertex(startProj, endProj); GeometryBase intersection = rayEdge.Intersection(edgeProj); if (intersection != null && intersection.GetType() == typeof(Vertex)) { return(centre.DistanceTo((Vertex)intersection)); } else { return(0); } }
protected override void SolveInstance(IGH_DataAccess DA) { GeometryBase G = null; string L = "预设值"; List <Curve> H = new List <Curve>(); Color C = Color.Black; DA.GetData(0, ref G); DA.GetData(1, ref L); DA.GetDataList(2, H); DA.GetData(3, ref C); int NOAH_BAKE_INFO = 0; try { PythonScript script = PythonScript.Create(); script.ExecuteScript("import scriptcontext as sc\nV=sc.sticky['NOAH_BAKE_INFO']"); NOAH_BAKE_INFO = (int)script.GetVariable("V"); } catch { NOAH_BAKE_INFO = 0; } if (NOAH_BAKE_INFO == 1) { if (G != null && H.Count == 0) { //写入物件 ObjectAttributes att = getObjAttr(L, rhinoDoc, C); switch (G.ObjectType) { case ObjectType.Brep: rhinoDoc.Objects.AddBrep(G as Brep, att); break; case ObjectType.Curve: rhinoDoc.Objects.AddCurve(G as Curve, att); break; case ObjectType.Point: rhinoDoc.Objects.AddPoint((G as Rhino.Geometry.Point).Location, att); break; case ObjectType.Surface: rhinoDoc.Objects.AddSurface(G as Surface, att); break; case ObjectType.Mesh: rhinoDoc.Objects.AddMesh(G as Mesh, att); break; case ObjectType.PointSet: rhinoDoc.Objects.AddPointCloud(G as Rhino.Geometry.PointCloud, att); //This is a speculative entry break; default: AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "不能识别的物体: " + G.GetType().FullName); break; } } else if (G == null && H.Count > 0) { ObjectAttributes att = getObjAttr(L, rhinoDoc, C); Hatch[] hatches = Hatch.Create(H, 0, 0, 1, 0); foreach (Hatch hatch in hatches) { rhinoDoc.Objects.AddHatch(hatch, att); } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "G和H只能输入一个"); } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "等待写入"); } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { /* * Original Version Written by Giulio Piacentino - 2010 11 21 - for Grasshopper 0.8.002 * Enhanced by Co-de-iT (Alessio) - now bakes on chosen layer(s) and in groups */ GeometryBase obj = null; if (!DA.GetData(0, ref obj)) { return; } string name = ""; DA.GetData(1, ref name); string layer = ""; DA.GetData(2, ref layer); Color color = new Color(); DA.GetData(3, ref color); Object material = new Object(); DA.GetData(4, ref material); int group_n = 0; DA.GetData(5, ref group_n); bool group = false; DA.GetData(6, ref group); bool bake_iT = false; DA.GetData(7, ref bake_iT); if (!bake_iT) { return; } //Make new attribute to set name Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes(); //Set object name if (!string.IsNullOrEmpty(name)) { att.Name = name; } //Set color if (!color.IsEmpty) { att.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject; //Make the color type "by object" att.ObjectColor = color; att.PlotColorSource = Rhino.DocObjects.ObjectPlotColorSource.PlotColorFromObject; //Make the plot color type "by object" att.PlotColor = color; } // Set group if (group) { Rhino.RhinoDoc.ActiveDoc.Groups.Add(Convert.ToString(group_n)); att.AddToGroup(group_n); } //Set layer if (!string.IsNullOrEmpty(layer) && Rhino.DocObjects.Layer.IsValidName(layer)) { //Get the current layer index Rhino.DocObjects.Tables.LayerTable layerTable = Rhino.RhinoDoc.ActiveDoc.Layers; //int layerIndex = layerTable.Find(layer, true); int layerIndex = layerTable.FindByFullPath(layer, -1); if (layerIndex < 0) //This layer does not exist, we add it { Rhino.DocObjects.Layer onlayer = new Rhino.DocObjects.Layer(); //Make a new layer onlayer.Name = layer; onlayer.Color = Color.Gainsboro; // sets new layer color - future dev: choose new layers color layerIndex = layerTable.Add(onlayer); //Add the layer to the layer table if (layerIndex > -1) //We managed to add the layer! { att.LayerIndex = layerIndex; //Print("Added new layer to the document at position " + layerIndex + " named " + layer + ". "); } //else //Print("Layer did not add. Try cleaning up your layers."); //This never happened to me. } else { att.LayerIndex = layerIndex; //We simply add to the existing layer } } //Set plotweight //if (pWidth > 0) //{ // att.PlotWeightSource = Rhino.DocObjects.ObjectPlotWeightSource.PlotWeightFromObject; // att.PlotWeight = pWidth; //} //Set material bool materialByName = !string.IsNullOrEmpty(material as string); Rhino.Display.DisplayMaterial inMaterial; if (material is GH_Material) { GH_Material gMat = material as GH_Material; inMaterial = gMat.Value as Rhino.Display.DisplayMaterial; } else { inMaterial = material as Rhino.Display.DisplayMaterial; } if (material is Color) { inMaterial = new Rhino.Display.DisplayMaterial((Color)material); } if (material != null && inMaterial == null && !materialByName) { if (!(material is string)) { try //We also resort to try with IConvertible { inMaterial = (Rhino.Display.DisplayMaterial)Convert.ChangeType(material, typeof(Rhino.Display.DisplayMaterial)); } catch (InvalidCastException) { } } } if (inMaterial != null || materialByName) { string matName; if (!materialByName) { matName = string.Format("D:{0}-E:{1}-S:{2},{3}-T:{4}", Format(inMaterial.Diffuse), Format(inMaterial.Emission), Format(inMaterial.Specular), inMaterial.Shine.ToString(), inMaterial.Transparency.ToString() ); } else { matName = (string)material; } int materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Find(matName, true); if (materialIndex < 0 && !materialByName) //Material does not exist and we have its specs { materialIndex = Rhino.RhinoDoc.ActiveDoc.Materials.Add(); //Let's add it if (materialIndex > -1) { //Print("Added new material at position " + materialIndex + " named \"" + matName + "\". "); Rhino.DocObjects.Material m = Rhino.RhinoDoc.ActiveDoc.Materials[materialIndex]; m.Name = matName; m.DiffuseColor = inMaterial.Diffuse; m.EmissionColor = inMaterial.Emission; //m.ReflectionColor = inMaterial.Specular; m.SpecularColor = inMaterial.Specular; m.Shine = inMaterial.Shine; m.Transparency = inMaterial.Transparency; //m.TransparentColor = no equivalent m.CommitChanges(); att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject; att.MaterialIndex = materialIndex; } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Material did not add. Try cleaning up your materials."); //This never happened to me. } } else if (materialIndex < 0 && materialByName) //Material does not exist and we do not have its specs. We do nothing { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Warning: material name not found. I cannot set the source to this material name. Add a material with name: " + matName); } else { //If this material exists, we do not replace it! att.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject; att.MaterialIndex = materialIndex; } } //Set wire density //if (wires == -1 || wires > 0) //{ // att.WireDensity = wires; //} //Bake to the right type of object switch (obj.ObjectType) { case Rhino.DocObjects.ObjectType.Brep: Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(obj as Brep, att); break; case Rhino.DocObjects.ObjectType.Curve: Rhino.RhinoDoc.ActiveDoc.Objects.AddCurve(obj as Curve, att); break; case Rhino.DocObjects.ObjectType.Point: Rhino.RhinoDoc.ActiveDoc.Objects.AddPoint((obj as Rhino.Geometry.Point).Location, att); break; case Rhino.DocObjects.ObjectType.Surface: Rhino.RhinoDoc.ActiveDoc.Objects.AddSurface(obj as Surface, att); break; case Rhino.DocObjects.ObjectType.Mesh: Rhino.RhinoDoc.ActiveDoc.Objects.AddMesh(obj as Mesh, att); break; case Rhino.DocObjects.ObjectType.Extrusion: typeof(Rhino.DocObjects.Tables.ObjectTable).InvokeMember("AddExtrusion", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod, null, Rhino.RhinoDoc.ActiveDoc.Objects, new object[] { obj, att }); break; case Rhino.DocObjects.ObjectType.PointSet: Rhino.RhinoDoc.ActiveDoc.Objects.AddPointCloud(obj as Rhino.Geometry.PointCloud, att); //This is a speculative entry break; default: AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "The script does not know how to handle this type of geometry: " + obj.GetType().FullName); break; } }