Exemple #1
0
 void attributesTable_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
 {
     if (e.Action == DataRowAction.Add)
     {
         Geometries.Add(((FeatureDataRow)e.Row).Geometry);
     }
 }
Exemple #2
0
 private void OnOgrGeometryChanged()
 {
     _ignoreGeoChanged   = true;
     _ignoreCoordChanged = true;
     Geometries.Clear();
     Coordinates.Clear();
     if (OgrGeometry != null)
     {
         var geometryCount = OgrGeometry.GetGeometryCount();
         if (geometryCount == 0)
         {
             var pointCount = OgrGeometry.GetPointCount();
             var dimension  = OgrGeometry.GetCoordinateDimension();
             for (int i = 0; i < pointCount; i++)
             {
                 double[] argout = new double[dimension];
                 OgrGeometry.GetPoint(i, argout);
                 Coordinate coordinate = new Coordinate(argout);
                 Coordinates.Add(coordinate);
             }
         }
         else
         {
             for (int i = 0; i < geometryCount; i++)
             {
                 var      ogrGeometry = OgrGeometry.GetGeometryRef(i);
                 Geometry geometry    = new Geometry(ogrGeometry);
                 Geometries.Add(geometry);
             }
         }
     }
     _ignoreGeoChanged   = false;
     _ignoreCoordChanged = false;
 }
        //CONSTRUCTOR
        public Design(RadicalComponent component)
        {
            //Access the component
            this.MyComponent = component;

            this.Variables   = new List <IVariable>();
            this.Geometries  = new List <IDesignGeometry>();
            this.Constraints = new List <Constraint>();

            // ADD VARIABLES
            //Sliders
            foreach (IGH_Param param in MyComponent.Params.Input[2].Sources)
            {
                SliderVariable s = new SliderVariable(param);
                if (s.CurrentValue == 0)
                {
                    if (s.Max >= 0.001)
                    {
                        s.UpdateValue(0.001);
                    }
                    else
                    {
                        s.UpdateValue(-0.001);
                    }
                }
                this.Variables.Add(new SliderVariable(param));
            }
            Grasshopper.Instances.ActiveCanvas.Document.NewSolution(false, Grasshopper.Kernel.GH_SolutionMode.Silent);

            //Surfaces
            for (int i = 0; i < MyComponent.Params.Input[3].Sources.Count; i++)
            {
                IGH_Param    param = MyComponent.Params.Input[3].Sources[i];
                NurbsSurface surf  = MyComponent.SrfVariables[i];
                Geometries.Add(new DesignSurface(param, surf));
            }
            //Curves
            for (int i = 0; i < MyComponent.Params.Input[4].Sources.Count; i++)
            {
                IGH_Param  param = MyComponent.Params.Input[4].Sources[i];
                NurbsCurve curv  = MyComponent.CrvVariables[i];
                this.Geometries.Add(new DesignCurve(param, curv));
            }

            // Add geometries to variables list
            // not the cleanest way to do it, review code structure
            if (Geometries.Any())
            {
                this.Variables.AddRange(Geometries.Select(x => x.Variables).SelectMany(x => x).ToList());
            }

            // ADD CONSTRAINTS
            for (int i = 0; i < component.Constraints.Count; i++)
            {
                this.Constraints.Add(new Constraint(MyComponent, Constraint.ConstraintType.morethan, i));
            }

            MyComponent.numVars = this.Variables.Where(var => var.IsActive).Count();
        }
Exemple #4
0
        public GeometryViewModel()
        {
            Geometries = AUTDSettings.Instance.GeometriesReactive;

            Current    = new ReactivePropertySlim <GeometrySettingReactive?>();
            AddItem    = new ReactiveCommand();
            RemoveItem = Current.Select(c => c != null).ToReactiveCommand();
            UpItem     = Current.Select(c => c != null && c.No.Value != 0).ToReactiveCommand();
            DownItem   = Current.Select(c => c != null && c.No.Value != Geometries.Count - 1).ToReactiveCommand();

            AddItem.Subscribe(_ =>
            {
                var item = new GeometrySettingReactive(Geometries.Count);
                Geometries.Add(item);
                Current.Value = item;
            });
            RemoveItem.Subscribe(_ =>
            {
                if (Current.Value == null)
                {
                    return;
                }

                var delNo = Current.Value.No.Value;
                Geometries.RemoveAt(delNo);
                ResetNo();
                Current.Value = Geometries.Count > delNo ? Geometries[delNo] : Geometries.Count > 0 ? Geometries[delNo - 1] : null;
            });
            UpItem.Subscribe(_ =>
            {
                if (Current.Value == null)
                {
                    return;
                }

                var cNo = Current.Value.No.Value;
                var up  = Geometries[cNo - 1];
                Geometries.Insert(cNo - 1, Current.Value);
                Geometries.RemoveAt(cNo);
                Geometries[cNo] = up;
                ResetNo();
                Current.Value = Geometries[cNo - 1];
            });
            DownItem.Subscribe(_ =>
            {
                if (Current.Value == null)
                {
                    return;
                }

                var cNo  = Current.Value.No.Value;
                var down = Geometries[cNo + 1];
                Geometries.RemoveAt(cNo + 1);
                Geometries.Insert(cNo + 1, Current.Value);
                Geometries[cNo] = down;
                ResetNo();
                Current.Value = Geometries[cNo + 1];
            });
        }
Exemple #5
0
 public void Attach(Geometry3D Geometry)
 {
     if (!Geometries.ContainsKey(Geometry))
     {
         Geometries.Add(Geometry, Geometry.Name);
         Count = Geometries.Count;
     }
 }
Exemple #6
0
        /*
         * public void AttachRange(List<Node3D> NodeList)
         * {
         *  for (int i = 0; i < NodeList.Count; i++)
         *  {
         *      if (!Children.ContainsKey(NodeList[i]))
         *      {
         *          Children.Add(NodeList[i], NodeList[i].Name);
         *      }
         *  }
         * }
         */

        public void AttachRange(List <Geometry3D> GeometryList)
        {
            for (int i = 0; i < GeometryList.Count; i++)
            {
                if (!Geometries.ContainsKey(GeometryList[i]))
                {
                    Geometries.Add(GeometryList[i], GeometryList[i].Name);
                }
            }
            Count = Geometries.Count;
        }
        public void AddVisual(IDrawingVisual dv)
        {
            if (dv is ICADEnitiyVisual)
            {
                Geometries.Add((ICADEnitiyVisual)dv);
            }
            else
            {
                _tempVisuals.Add(dv);
            }

            AddVisualChild((Visual)dv);
            AddLogicalChild((Visual)dv);
        }
Exemple #8
0
        public IgesGeneralSymbol(IgesGeneralNote note, IEnumerable <IgesLeader> leaders, IEnumerable <IgesEntity> geometries)
            : this()
        {
            Note = note;
            foreach (var leader in leaders)
            {
                Leaders.Add(leader);
            }

            foreach (var geometry in geometries)
            {
                Geometries.Add(geometry);
            }

            if (Geometries.Count < 1)
            {
                throw new InvalidOperationException($"At least one geometrical entity must be specified.");
            }
        }
Exemple #9
0
        internal override int ReadParameters(List <string> parameters, IgesReaderBinder binder)
        {
            var index = 0;

            binder.BindEntity(Integer(parameters, index++), e => Note = e as IgesGeneralNote);
            var geometryCount = Integer(parameters, index++);

            for (int i = 0; i < geometryCount; i++)
            {
                binder.BindEntity(Integer(parameters, index++), e => Geometries.Add(e));
            }

            var leaderCount = Integer(parameters, index++);

            for (int i = 0; i < leaderCount; i++)
            {
                binder.BindEntity(Integer(parameters, index++), e => Leaders.Add(e as IgesLeader));
            }

            return(index);
        }
Exemple #10
0
        public Design(DSOptimizerComponent component)
        {
            //Access the component
            this.MyComponent = component;

            this.Variables   = new List <IVariable>();
            this.Geometries  = new List <IDesignGeometry>();
            this.Constraints = new List <Constraint>();

            // ADD VARIABLES
            //Sliders
            foreach (IGH_Param param in component.Params.Input[2].Sources)
            {
                this.Variables.Add(new SliderVariable(param));
            }
            //Curves
            for (int i = 0; i < component.Params.Input[3].Sources.Count; i++)
            {
                IGH_Param    param = component.Params.Input[3].Sources[i];
                NurbsSurface surf  = component.SrfVariables[i];
                Geometries.Add(new DesignSurface(param, surf));
            }
            //Surfaces
            for (int i = 0; i < component.Params.Input[4].Sources.Count; i++)
            {
                IGH_Param  param = component.Params.Input[4].Sources[i];
                NurbsCurve surf  = component.CrvVariables[i];
                this.Geometries.Add(new DesignCurve(param, surf));
            }

            // ADD CONSTRAINTS
            for (int i = 0; i < component.Constraints.Count; i++)
            {
                this.Constraints.Add(new Constraint(component, ConstraintType.morethan, i));
            }
        }
Exemple #11
0
        /// <summary>
        /// Loads an OBJ file and its related textures (if present) into this Mesh object.
        /// </summary>
        /// <param name="path">The full path of the OBJ file.</param>
        /// <returns>True if the OBJ file was successfully parsed, false if something went wrong.</returns>
        public bool Load(string path)
        {
            if (IsLoaded && Filename == path)
            {
                return(true);
            }

            if (!File.Exists(path))
            {
                return(false);
            }

            ClearAll();

            string objFileName = Path.GetFileName(path);
            string basePath    = Path.GetDirectoryName(path);

            float minX, minY, minZ;

            minX = minY = minZ = float.MaxValue;

            float maxX, maxY, maxZ;

            maxX = maxY = maxZ = float.MinValue;

            using (var t = new ScopeTimer($"Mesh.Load: {objFileName}"))
            {
                using (var fileStream = File.OpenRead(Path.Combine(basePath, objFileName)))
                {
                    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096))
                    {
                        try
                        {
                            string line = null;
                            bool   allIndicesAreAligned = true;

                            Geometry currentGeometry = new Geometry();

                            while ((line = streamReader.ReadLine()) != null)
                            {
                                if (string.IsNullOrWhiteSpace(line) || line.Length < 2)
                                {
                                    continue;
                                }

                                var tokens = line.Split(' ');

                                switch (tokens[0])
                                {
                                // Mtl File Reference
                                case "mtllib":
                                    if (tokens.Length > 2)
                                    {
                                        throw new Exception("MTL file references must have only 2 tokens.");
                                    }

                                    Materials = ParseMtlFile(basePath, tokens[1]);

                                    break;

                                // Texture Reference
                                case "usemtl":
                                    if (tokens.Length > 2)
                                    {
                                        throw new Exception("Texture references must have only 2 tokens.");
                                    }

                                    currentGeometry = new Geometry(tokens[1]);
                                    Geometries.Add(currentGeometry);

                                    break;

                                // Vertex
                                case "v":
                                    if (tokens.Length != 4 && tokens.Length != 7)
                                    {
                                        throw new Exception("Vertices must have either 4 or 7 tokens.");
                                    }

                                    Vector3 newVertex = new Vector3(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]));
                                    newVertex = newVertex.ConvertBetweenCoordinateSystems();
                                    Vertices.Add(newVertex);

                                    minX = Math.Min(minX, newVertex.X);
                                    minY = Math.Min(minY, newVertex.Y);
                                    minZ = Math.Min(minZ, newVertex.Z);

                                    maxX = Math.Max(maxX, newVertex.X);
                                    maxY = Math.Max(maxY, newVertex.Y);
                                    maxZ = Math.Max(maxZ, newVertex.Z);

                                    if (tokens.Length == 7)
                                    {
                                        Colors.Add(new Color(float.Parse(tokens[4]), float.Parse(tokens[5]),
                                                             float.Parse(tokens[6])));
                                    }

                                    break;

                                // Normal
                                case "vn":
                                    if (tokens.Length != 4)
                                    {
                                        throw new Exception("Vertex normals must have exactly 4 tokens.");
                                    }

                                    Vector3 newNormal = new Vector3(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]));
                                    newNormal = newNormal.ConvertBetweenCoordinateSystems();
                                    Normals.Add(newNormal);

                                    break;

                                // Texture Coordinates (UV)
                                case "vt":
                                    if (tokens.Length != 3)
                                    {
                                        throw new Exception("Texture coordinates must have exactly 3 tokens.");
                                    }

                                    UV.Add(new Vector2(float.Parse(tokens[1]), 1.0f - float.Parse(tokens[2])));

                                    break;

                                // Face
                                case "f":
                                    if (tokens.Length != 4)
                                    {
                                        throw new Exception("Every face must have exactly 4 tokens.");
                                    }

                                    // [0] = Vertex Index, [1] = Texture Coordinate (UV), [2] = Normal Index
                                    var v1 = tokens[1].Split('/');
                                    var v2 = tokens[2].Split('/');
                                    var v3 = tokens[3].Split('/');

                                    // Each face token is a triplet, with exactly 3 sub-tokens
                                    if (v1.Length != 3 || v2.Length != 3 || v3.Length != 3)
                                    {
                                        throw new Exception("Every face token must be a triplet with exactly 3 sub-tokens.");
                                    }

                                    // OBJ indexing starts at 1, so subtract 1 for each to get correct list index
                                    uint i1  = uint.Parse(v1[0]) - 1;
                                    uint iu1 = uint.TryParse(v1[1], out uint uv1) ? uv1 - 1 : 0;
                                    uint in1 = uint.Parse(v1[2]) - 1;

                                    uint i2  = uint.Parse(v2[0]) - 1;
                                    uint iu2 = uint.TryParse(v2[1], out uint uv2) ? uv2 - 1 : 0;
                                    uint in2 = uint.Parse(v2[2]) - 1;

                                    uint i3  = uint.Parse(v3[0]) - 1;
                                    uint iu3 = uint.TryParse(v3[1], out uint uv3) ? uv3 - 1 : 0;
                                    uint in3 = uint.Parse(v3[2]) - 1;

                                    Triangle tri = new Triangle(i1, iu1, in1, i2, iu2, in2, i3, iu3, in3);

                                    if (!tri.IsNormalAligned)
                                    {
                                        allIndicesAreAligned = false;
                                    }

                                    tri.ConvertBetweenCoordinateSystems();
                                    currentGeometry.Triangles.Add(tri);
                                    TriangleCount++;

                                    break;

                                default:
                                    break;
                                }
                            }

                            // If <= 1 texture was used, it won't have been added while parsing (no "usemtl" defined in OBJ)
                            if (currentGeometry.MaterialName == Geometry.DefaultMaterialName)
                            {
                                Geometries.Add(currentGeometry);
                            }

                            if (!allIndicesAreAligned)
                            {
                                AlignNormals();
                            }

                            // Check to make sure the mesh is valid
                            if (Vertices.Count != Normals.Count)
                            {
                                throw new Exception("Vertex count doesn't not match normal count.");
                            }

                            if (Colors.Count > 0 && Colors.Count != Vertices.Count)
                            {
                                throw new Exception("Color count does not match vertex count.");
                            }

                            foreach (var g in Geometries)
                            {
                                foreach (var tri in g.Triangles)
                                {
                                    if (tri.I1 > Vertices.Count || tri.I2 > Vertices.Count || tri.I3 > Vertices.Count)
                                    {
                                        throw new Exception("Triangle vertex index out of bounds.");
                                    }

                                    if (tri.In1 > Normals.Count || tri.In2 > Normals.Count || tri.In3 > Normals.Count)
                                    {
                                        throw new Exception("Triangle normal index out of bounds.");
                                    }

                                    if (UV.Count > 0 && !(tri.Iu1 < UV.Count && tri.Iu2 < UV.Count && tri.Iu3 < UV.Count))
                                    {
                                        throw new Exception("Triangle uv index out of bounds.");
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            ClearAll();

                            Debug.WriteLine($"Failed to parse {objFileName}. Exception: {e.Message}");
                            return(false);
                        }
                    }
                }
            }

            BoundingBox = new BoundingBox(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ));

            Debug.WriteLine($"Mesh loaded with {Vertices.Count} Vertices, {Normals.Count} Normals, {UV.Count} UV's, {Colors.Count} Colors, {TriangleCount} Triangles, {Geometries.Count} Geometries");

            IsLoaded = true;
            Filename = path;

            return(true);
        }