Beispiel #1
0
    public static IdPath From(string str)
    {
        var result = new IdPath();

        result.Parse(str);
        return(result);
    }
Beispiel #2
0
    public override void Read(XmlNode xml)
    {
        ids.Clear();
        if (Enum.GetNames(optionInternal.GetType()).Length >= 2)
        {
            Enum output = optionInternal;
            xml.Attributes["chirality"].Value.ToEnum(ref output);
            optionInternal = output;
        }
        foreach (XmlNode node in xml.ChildNodes)
        {
            if (node.Name != "entity" && node.Name != "link")
            {
                continue;
            }
            var        path = IdPath.From(node.Attributes["path"].Value);
            ICADObject o    = null;
            if (sketch.idMapping != null)
            {
                o = sketch.GetChild(sketch.idMapping[path.path.Last()]);
            }
            else
            {
                o = sketch.feature.detail.GetObjectById(path);
            }

            AddObject(o);
        }
        base.Read(xml);
    }
Beispiel #3
0
    public IdPath Clone()
    {
        var result = new IdPath();

        result.path = new List <Id>(path);
        return(result);
    }
Beispiel #4
0
    public bool RemoveById(IdPath idp)
    {
        var obj = detail.GetObjectById(idp);

        if (obj is SketchObject)
        {
            var sko = obj as SketchObject;
            sko.Destroy();
            return(true);
        }
        return(false);
    }
Beispiel #5
0
    public void ReadXml(string xml, bool readView = true, bool activateLast = true)
    {
        activeFeature = null;
        IdPath active = null;

        detail.ReadXml(xml, readView, out active);
        if (active.IsNull())
        {
            active = detail.features.Last().id;
        }
        UpdateFeatures();
        ActivateFeature(active);
    }
Beispiel #6
0
    public void ReadXml(string str, bool readView, out IdPath active)
    {
        Clear();
        var xml = new XmlDocument();

        xml.LoadXml(str);

        if (xml.DocumentElement.Attributes["id"] != null)
        {
            guid_ = idGenerator.Create(0);
        }

        if (readView)
        {
            if (xml.DocumentElement.Attributes["viewPos"] != null)
            {
                Camera.main.transform.position = xml.DocumentElement.Attributes["viewPos"].Value.ToVector3();
            }
            if (xml.DocumentElement.Attributes["viewRot"] != null)
            {
                Camera.main.transform.rotation = xml.DocumentElement.Attributes["viewRot"].Value.ToQuaternion();
            }
            if (xml.DocumentElement.Attributes["viewSize"] != null)
            {
                Camera.main.orthographicSize = xml.DocumentElement.Attributes["viewSize"].Value.ToFloat();
            }
        }

        if (xml.DocumentElement.Attributes["activeFeature"] != null)
        {
            active = IdPath.From(xml.DocumentElement.Attributes["activeFeature"].Value);
        }
        else
        {
            active = null;
        }

        foreach (XmlNode node in xml.DocumentElement)
        {
            if (node.Name != "feature")
            {
                continue;
            }
            var type = node.Attributes["type"].Value;
            var item = Type.GetType(type).GetConstructor(new Type[0]).Invoke(new object[0]) as Feature;
            AddFeature(item);
            item.Read(node);
        }
    }
Beispiel #7
0
 public override void Read(XmlNode xml)
 {
     ids.Clear();
     foreach (XmlNode node in xml.ChildNodes)
     {
         if (node.Name != "entity")
         {
             continue;
         }
         var path = IdPath.From(node.Attributes["path"].Value);
         var e    = sketch.feature.detail.GetObjectById(path) as IEntity;
         AddEntity(e);
     }
     base.Read(xml);
 }
Beispiel #8
0
    public virtual ICADObject GetObjectById(IdPath id, int index = 0)
    {
        if (id.path.Count == 0)
        {
            return(null);
        }
        var r  = GetChild(id.path[index]);
        var co = r as CADObject;

        if (co == null || index + 1 >= id.path.Count)
        {
            return(r);
        }
        return(co.GetObjectById(id, index + 1));
    }
Beispiel #9
0
    public override ICADObject GetObjectById(IdPath id, int index)
    {
        if (id.path[index].value == -1)
        {
            return(base.GetObjectById(id, index));
        }

        var arrayIndex = id.path[index].value;
        var entity     = detail.GetObjectById(id, index + 1) as IEntity;

        if (entity == null)
        {
            return(null);
        }
        return(new ArrayEntity(entity, this, arrayIndex));
    }
Beispiel #10
0
        public object GetPropertyValue(RowItem rowItem, RowKey rowKey)
        {
            while (!IdPath.StartsWith(rowItem.SublistId))
            {
                rowItem = rowItem.Parent;
            }
            if (IdPath.Equals(rowItem.SublistId))
            {
                return(rowItem.Value);
            }
            var parentValue = Parent.GetPropertyValue(rowItem, rowKey);

            if (parentValue == null)
            {
                return(null);
            }
            return(GetPropertyValueFromParent(parentValue, rowKey));
        }
Beispiel #11
0
    public IdPath GetRelativePath(CADObject from)
    {
        var result = new IdPath();
        var p      = this;

        while (p != null)
        {
            if (p == from)
            {
                return(result);
            }
            if (p.guid == Id.Null)
            {
                return(result);
            }
            result.path.Add(p.guid);
            p = p.parentObject;
        }
        return(result);
    }
Beispiel #12
0
 public override void Read(XmlNode xml)
 {
     ids.Clear();
     if (Enum.GetNames(optionInternal.GetType()).Length >= 2)
     {
         Enum output = optionInternal;
         xml.Attributes["chirality"].Value.ToEnum(ref output);
         optionInternal = output;
     }
     foreach (XmlNode node in xml.ChildNodes)
     {
         if (node.Name != "entity")
         {
             continue;
         }
         var path = IdPath.From(node.Attributes["path"].Value);
         var o    = sketch.feature.detail.GetObjectById(path);
         AddObject(o);
     }
     base.Read(xml);
 }
Beispiel #13
0
    public ICADObject GetObjectById(IdPath id)
    {
        var i = -1;
        var p = this;

        while (true)
        {
            if (p.guid == Id.Null)
            {
                i = id.path.Count;
                break;
            }
            //i = id.path.FindLastIndex(g => g == p.guid);
            //if(i != -1) break;
            p = p.parentObject;
            if (p == null)
            {
                return(null);
            }
        }
        ICADObject r = p;

        while (i > 0)
        {
            i--;
            var co = r as CADObject;
            if (co == null)
            {
                return(null);
            }
            r = co.GetChild(id.path[i]);
            if (p == null)
            {
                return(null);
            }
        }
        return(r);
    }
Beispiel #14
0
    public static Solid CreateSolidExtrusion(List <List <Entity> > entitiyLoops, float extrude, UnityEngine.Matrix4x4 tf, IdPath feature)
    {
        var  ids      = new List <List <Id> >();
        var  polygons = Sketch.GetPolygons(entitiyLoops, ref ids);
        bool inversed = extrude < 0f;

        var polys = new List <Polygon>();

        Func <Vector3, Vector3> TF = v => tf.MultiplyPoint(v);

        for (int pi = 0; pi < polygons.Count; pi++)
        {
            var p         = polygons[pi];
            var pid       = ids[pi];
            var pv        = new List <Vector3>(p);
            var triangles = Triangulation.Triangulate(pv);

            IdPath polyId        = feature.With(new Id(-1));
            bool   invComp       = true;
            var    shift         = Vector3.zero;
            var    extrudeVector = Vector3.forward * extrude;

            for (int side = 0; side < 2; side++)
            {
                for (int i = 0; i < triangles.Count / 3; i++)
                {
                    var polygonVertices = new List <Vertex>();
                    for (int j = 0; j < 3; j++)
                    {
                        polygonVertices.Add(TF(triangles[i * 3 + j] + shift).ToVertex());
                    }
                    if (inversed == invComp)
                    {
                        polygonVertices.Reverse();
                    }
                    polys.Add(new Polygon(polygonVertices, polyId));
                }
                polyId  = feature.With(new Id(-2));
                invComp = false;
                shift   = extrudeVector;
            }

            Dictionary <Id, IdPath> paths = new Dictionary <Id, IdPath>();
            for (int i = 0; i < p.Count; i++)
            {
                var polygonVertices = new List <Vertex>();
                polygonVertices.Add(TF(p[i]).ToVertex());
                polygonVertices.Add(TF(p[(i + 1) % p.Count]).ToVertex());
                polygonVertices.Add(TF((p[(i + 1) % p.Count] + extrudeVector)).ToVertex());
                polygonVertices.Add(TF((p[i] + extrudeVector)).ToVertex());
                if (!inversed)
                {
                    polygonVertices.Reverse();
                }
                IdPath curPath = null;
                if (!paths.ContainsKey(pid[i]))
                {
                    curPath = feature.With(pid[i]);
                    paths.Add(pid[i], curPath);
                }
                else
                {
                    curPath = paths[pid[i]];
                }
                polys.Add(new Polygon(polygonVertices, curPath));
            }
        }
        return(Solid.FromPolygons(polys));
    }
Beispiel #15
0
    public void ActivateFeature(IdPath path)
    {
        var feature = (Feature)detail.GetObjectById(path);

        ActivateFeature(feature);
    }
Beispiel #16
0
    public static Solid CreateSolidRevolve(List <List <Entity> > entitiyLoops, float angle, float helixStep, Vector3 axis, Vector3 origin, float angleStep, UnityEngine.Matrix4x4 tf, IdPath feature)
    {
        var  ids      = new List <List <Id> >();
        var  polygons = Sketch.GetPolygons(entitiyLoops, ref ids);
        bool isHelix  = (Math.Abs(helixStep) > 1e-6);

        if (!isHelix && Mathf.Abs(angle) > 360f)
        {
            angle = Mathf.Sign(angle) * 360f;
        }
        bool inversed = angle < 0f;
        int  subdiv   = (int)Mathf.Ceil(Math.Abs(angle) / angleStep);
        var  drot     = UnityEngine.Matrix4x4.Translate(origin) * UnityEngine.Matrix4x4.Rotate(Quaternion.AngleAxis(angle / subdiv, axis)) * UnityEngine.Matrix4x4.Translate(-origin);

        Func <float, Vector3, Vector3> PointOn = (float a, Vector3 point) => {
            var ax  = axis;
            var axn = axis.normalized;
            var t   = a / 360.0f;
            var o   = origin;
            var prj = ExpVector.ProjectPointToLine(point, o, o + ax);
            var ra  = Mathf.Atan2(helixStep / 4.0f, (point - prj).magnitude);
            var res = ExpVector.RotateAround(point, point - prj, o, ra);
            res = ExpVector.RotateAround(res, ax, o, a * Mathf.PI / 180.0f);
            return(res + axn * t * helixStep);
        };

        var polys = new List <Polygon>();
        Func <Vector3, Vector3> TF = v => tf.MultiplyPoint(v);

        for (int pi = 0; pi < polygons.Count; pi++)
        {
            var p         = polygons[pi];
            var pid       = ids[pi];
            var pv        = new List <Vector3>(p);
            var triangles = Triangulation.Triangulate(pv);

            IdPath polyId  = feature.With(new Id(-1));
            bool   invComp = true;

            if (Math.Abs(Math.Abs(angle) - 360f) > 1e-6 || isHelix)
            {
                float a = 0f;
                for (int side = 0; side < 2; side++)
                {
                    for (int i = 0; i < triangles.Count / 3; i++)
                    {
                        var polygonVertices = new List <Vertex>();
                        for (int j = 0; j < 3; j++)
                        {
                            polygonVertices.Add(PointOn(a, TF(triangles[i * 3 + j])).ToVertex());
                        }
                        if (inversed == invComp)
                        {
                            polygonVertices.Reverse();
                        }
                        polys.Add(new Polygon(polygonVertices, polyId));
                    }
                    polyId  = feature.With(new Id(-2));
                    invComp = false;
                    a       = angle;
                }
            }

            Dictionary <Id, IdPath> paths = new Dictionary <Id, IdPath>();
            for (int i = 0; i < p.Count; i++)
            {
                float a  = 0f;
                float da = angle / subdiv;
                for (int j = 0; j < subdiv; j++)
                {
                    var polygonVertices = new List <Vertex>();
                    polygonVertices.Add(PointOn(a, TF(p[(i + 1) % p.Count])).ToVertex());
                    polygonVertices.Add(PointOn(a + da, TF(p[(i + 1) % p.Count])).ToVertex());
                    polygonVertices.Add(PointOn(a + da, TF(p[i])).ToVertex());
                    polygonVertices.Add(PointOn(a, TF(p[i])).ToVertex());
                    a += da;
                    if (!inversed)
                    {
                        polygonVertices.Reverse();
                    }
                    IdPath curPath = null;
                    if (!paths.ContainsKey(pid[i]))
                    {
                        curPath = feature.With(pid[i]);
                        paths.Add(pid[i], curPath);
                    }
                    else
                    {
                        curPath = paths[pid[i]];
                    }

                    if (isHelix)
                    {
                        var verts = new List <Vertex>();
                        verts.Add(polygonVertices[0]);
                        verts.Add(polygonVertices[1]);
                        verts.Add(polygonVertices[2]);
                        polys.Add(new Polygon(verts, curPath));

                        verts = new List <Vertex>();
                        verts.Add(polygonVertices[0]);
                        verts.Add(polygonVertices[2]);
                        verts.Add(polygonVertices[3]);
                        polys.Add(new Polygon(verts, curPath));
                    }
                    else
                    {
                        polys.Add(new Polygon(polygonVertices, curPath));
                    }
                }
            }
        }
        return(Solid.FromPolygons(polys));
    }