Esempio n. 1
0
        /// <summary>
        /// Converts a polyline into a GH_Curve
        /// </summary>
        /// <param name="pline">The polyline to convert</param>
        /// <returns>The GH_Curve</returns>
        public static GH_Curve ConvertToGHCurve(Polyline pline)
        {
            GH_Curve ghl = new GH_Curve();
            bool     c   = GH_Convert.ToGHCurve(pline, GH_Conversion.Both, ref ghl);

            return(ghl);
        }
Esempio n. 2
0
        private GH_Structure <IGH_Goo> SingleDataStructrue(object value)
        {
            if (value is string path)
            {
                if (File.Exists(path) && Path.GetExtension(path) == ".noahdata")
                {
                    byte[] array;
                    try
                    {
                        array = File.ReadAllBytes(path);
                    }
                    catch
                    {
                        return(null);
                    }

                    return(IO.DeserializeGrasshopperData(array));
                }
            }

            GH_Structure <IGH_Goo> m_data = new GH_Structure <IGH_Goo>();

            GH_Number castNumber = null;
            GH_String castString = null;
            GH_Curve  castCurve  = null;

            if (GH_Convert.ToGHCurve(value, GH_Conversion.Both, ref castCurve))
            {
                m_data.Append(new GH_ObjectWrapper(castCurve));
            }
            else if (GH_Convert.ToGHNumber(value, GH_Conversion.Both, ref castNumber))
            {
                m_data.Append(new GH_ObjectWrapper(castNumber));
            }
            else if (GH_Convert.ToGHString(value, GH_Conversion.Both, ref castString))
            {
                m_data.Append(new GH_ObjectWrapper(castString));
            }
            else
            {
                m_data.Append((IGH_Goo)value);
            }

            return(m_data);
        }
Esempio n. 3
0
        public static GH_Curve ProjectPolylineToTopo(Mesh topoMesh, Polyline pLine)
        {
            GH_Curve ghCurve = new GH_Curve();


            List <Point3d> projectedPts = new List <Point3d>();

            for (int j = 0; j < pLine.Count; j++)
            {
                Ray3d  ray = new Ray3d(pLine[j], moveDir);
                double t   = Rhino.Geometry.Intersect.Intersection.MeshRay(topoMesh, ray);
                if (t >= 0.0)
                {
                    projectedPts.Add(ray.PointAt(t));
                }
                else
                {
                    Ray3d  rayOpp = new Ray3d(pLine[j], -moveDir);
                    double tOpp   = Rhino.Geometry.Intersect.Intersection.MeshRay(topoMesh, rayOpp);
                    if (tOpp >= 0.0)
                    {
                        projectedPts.Add(rayOpp.PointAt(tOpp));
                    }
                    else
                    {
                        //return null;
                    }
                }
            }
            Polyline pLineOut = new Polyline(projectedPts);

            if (pLineOut.IsValid)
            {
                GH_Convert.ToGHCurve(pLineOut.ToNurbsCurve(), GH_Conversion.Primary, ref ghCurve);
                return(ghCurve);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 4
0
        public static GH_Structure <GH_Curve> GetJoined(IList <Brep> inBreps)
        {
            GH_Structure <GH_Curve> joinedCurves = new GH_Structure <GH_Curve>();

            for (int i = 0; i < inBreps.Count; i++)
            {
                GH_Path      path        = new GH_Path(i);
                Brep         brep        = inBreps[i];
                List <Curve> curvesToAdd = new List <Curve>();
                curvesToAdd.AddRange(BrepBorderExtractor.GetJoined(brep));

                foreach (Curve curve in curvesToAdd)
                {
                    GH_Curve ghCurve = null;
                    if (GH_Convert.ToGHCurve(curve, GH_Conversion.Both, ref ghCurve))
                    {
                        joinedCurves.Append(ghCurve, path);
                    }
                }
            }

            return(joinedCurves);
        }
Esempio n. 5
0
        public static void AssignDataToDoc(string dataSetJson)
        {
            JObject   dataSet      = JsonConvert.DeserializeObject <JObject>(dataSetJson);
            GH_Canvas activeCanvas = Instances.ActiveCanvas;

            if (activeCanvas == null)
            {
                throw new Exception("读取文档失败");
            }

            GH_Document doc = activeCanvas.Document;

            if (doc == null)
            {
                return;
            }

            var hooks = doc.ClusterInputHooks();

            foreach (var hook in hooks)
            {
                GH_Structure <IGH_Goo> m_data;

                string key = hook.NickName;

                if (string.IsNullOrEmpty(key))
                {
                    key = hook.Name;
                }
                if (string.IsNullOrEmpty(key))
                {
                    key = hook.CustomName;
                }
                if (string.IsNullOrEmpty(key))
                {
                    key = hook.CustomNickName;
                }

                if (!key.StartsWith("@", StringComparison.Ordinal))
                {
                    continue;
                }

                key = key.Substring(1);

                if (!dataSet.TryGetValue(key, out var data))
                {
                    continue;
                }

                m_data = SingleDataStructrue(data);

                hook.ClearPlaceholderData();
                hook.SetPlaceholderData(m_data);
                //hook.ExpireSolution(true);
            }

            // for data placeholder inside cluster (deep = 1)

            var clusters = new List <GH_Cluster>();

            foreach (var obj in doc.Objects)
            {
                if (!(obj is GH_Cluster cluster))
                {
                    continue;
                }
                clusters.Add(cluster);
            }

            if (clusters.Count == 0)
            {
                return;
            }


            foreach (var cluster in clusters)
            {
                foreach (var obj in cluster.Document("").Objects)
                {
                    if (!(obj is IGH_Param param))
                    {
                        continue;
                    }

                    string nickname = param.NickName;

                    if (string.IsNullOrEmpty(nickname))
                    {
                        nickname = param.Name;
                    }
                    if (!nickname.StartsWith("@", StringComparison.Ordinal))
                    {
                        continue;
                    }
                    nickname = nickname.Substring(1);
                    if (!dataSet.TryGetValue(nickname, out var data))
                    {
                        continue;
                    }


                    Utility.InvokeMethod(param, "Script_ClearPersistentData");
                    Utility.InvokeMethod(param, "Script_AddPersistentData", new List <object>()
                    {
                        data
                    });

                    //param.ExpireSolution(true);
                    //cluster.ExpireSolution(true);
                }
            }

            doc.NewSolution(true);

            activeCanvas.Document.IsModified = false;
            activeCanvas.Refresh();

            GH_Structure <IGH_Goo> SingleDataStructrue(object value)
            {
                GH_Structure <IGH_Goo> m_data = new GH_Structure <IGH_Goo>();

                GH_Number castNumber = null;
                GH_String castString = null;
                GH_Curve  castCurve  = null;

                if (GH_Convert.ToGHCurve(value, GH_Conversion.Both, ref castCurve))
                {
                    m_data.Append(new GH_ObjectWrapper(castCurve));
                }
                else if (GH_Convert.ToGHNumber(value, GH_Conversion.Both, ref castNumber))
                {
                    m_data.Append(new GH_ObjectWrapper(castNumber));
                }
                else if (GH_Convert.ToGHString(value, GH_Conversion.Both, ref castString))
                {
                    m_data.Append(new GH_ObjectWrapper(castString));
                }
                else
                {
                    m_data.Append((IGH_Goo)value);
                }

                return(m_data);
            }
        }
Esempio n. 6
0
        /// <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)
        {
            InputChecker inputChecker = new InputChecker(this);

            #region get Input From GH Canvas
            GH_Structure <GH_Curve> inCurves = new GH_Structure <GH_Curve>();
            bool areCurvesOK = DA.GetDataTree(0, out inCurves);
            inputChecker.StopIfConversionIsFailed(areCurvesOK);
            GH_Structure <GH_Curve> ghCurves = new GH_Structure <GH_Curve>();
            ghCurves = inCurves.Duplicate();
            ghCurves.Graft(GH_GraftMode.GraftAll);
            ghCurves.Simplify(GH_SimplificationMode.CollapseAllOverlaps);

            GH_Structure <GH_Number> inDistances = new GH_Structure <GH_Number>();
            bool areDistancesOk = DA.GetDataTree(1, out inDistances);
            inputChecker.StopIfConversionIsFailed(areDistancesOk);
            GH_Structure <GH_Number> ghDistances = new GH_Structure <GH_Number>();
            ghDistances = ValuesAllocator.NumsDSFromCurves(ghCurves, inDistances, ghDistances);

            GH_Structure <GH_Plane> inPlanes = new GH_Structure <GH_Plane>();
            bool arePlanesOk = DA.GetDataTree(2, out inPlanes);
            inputChecker.StopIfConversionIsFailed(arePlanesOk);
            GH_Structure <GH_Plane> ghPlanes = new GH_Structure <GH_Plane>();
            ghPlanes = ValuesAllocator.PlanesDSFromCurves(ghCurves, inPlanes, ghPlanes);

            GH_Structure <GH_Integer> inCorners = new GH_Structure <GH_Integer>();
            bool areCornerssOk = DA.GetDataTree(3, out inCorners);
            inputChecker.StopIfConversionIsFailed(areCornerssOk);
            GH_Structure <GH_Integer> ghCorners = new GH_Structure <GH_Integer>();
            ghCorners = ValuesAllocator.IntegerDSFromCurves(ghCurves, inCorners, ghCorners);
            #endregion

            GH_Structure <GH_Curve> ghCurveOffset = new GH_Structure <GH_Curve>();
            double docTollerance = DocumentTolerance();

            int pathIndex = 0;
            foreach (GH_Path ghPath in ghCurves.Paths)
            {
                for (int i = 0; i < ghCurves.get_Branch(ghPath).Count; i++)
                {
                    CurveOffsetCornerStyle cornerStyle = CurveOffsetCornerStyle.None;
                    int cornerStyleInInt = ghCorners.get_DataItem(ghPath, i).Value;
                    GetCornerStyleFromInt(ref cornerStyleInInt, ref cornerStyle);

                    Curve  crv   = ghCurves.get_DataItem(ghPath, i).Value;
                    Plane  plane = ghPlanes.get_DataItem(ghPath, i).Value;
                    double dist  = ghDistances.get_DataItem(ghPath, i).Value;

                    List <Curve> resultingCurves = new List <Curve>();

                    resultingCurves.AddRange(crv.Offset(plane, dist, docTollerance, cornerStyle));
                    resultingCurves.AddRange(crv.Offset(plane, dist *= -1, docTollerance, cornerStyle));
                    foreach (Curve resultingCrv in resultingCurves)
                    {
                        GH_Curve ghResultingCrv = null;
                        if (GH_Convert.ToGHCurve(resultingCrv, GH_Conversion.Both, ref ghResultingCrv))
                        {
                            ghCurveOffset.Append(ghResultingCrv, ghPath);
                        }
                    }
                }
                pathIndex++;
            }

            DA.SetDataTree(0, ghCurveOffset);
        }
Esempio n. 7
0
        /*******************************************/

        public static bool CastToGoo(object value, ref GH_Curve target)
        {
            return(GH_Convert.ToGHCurve(value, GH_Conversion.Both, ref target));
        }