Example #1
0
 public void Add(PartTessellation other)
 {
     Lines.AddRange(other.Lines);
     Meshes.AddRange(other.Meshes);
     Components.AddRange(other.Components);
 }
        protected override void OnExecute(Command command, ExecutionContext context, Rectangle buttonRect)
        {
            var window = Window.ActiveWindow;
            var document = window.Document;

            var surfaceDeviationCommand = Command.GetCommand("SurfaceDeviation");
            double surfaceDeviation;
            if (!double.TryParse(surfaceDeviationCommand.Text, out surfaceDeviation))
                surfaceDeviation = (new TessellationOptions()).SurfaceDeviation;

            var angleDeviationCommand = Command.GetCommand("AngleDeviation");
            double angleDeviation;
            if (!double.TryParse(angleDeviationCommand.Text, out angleDeviation))
                angleDeviation = (new TessellationOptions()).AngleDeviation;

            Part mainPart = (Part)Window.ActiveWindow.Scene;

            int i = 0;
            var partIDs = new Dictionary<Part, int>();

            foreach (var part in WalkParts(mainPart)) {
                if (partIDs.ContainsKey(part))
                    continue;

                partIDs[part] = i++;
            }

            var partList = new Part[partIDs.Count];

            foreach (var pair in partIDs)
                partList[pair.Value] = pair.Key;

            var partTessellations = new List<PartTessellation>();

            foreach (var part in partList) {
                var totalTessellation = new PartTessellation();
                foreach (var body in part.Bodies) {
                    if (!body.GetVisibility(null) ?? !body.Layer.IsVisible(null))
                        continue;

                    var masterBody = body.Shape;

                    PartTessellation tessellation;
                    Func<Face, Color> getColor = f => body.GetDesignFace(f).GetColor(null) ??
                                        body.GetColor(null) ??
                                        body.Layer.GetColor(null);
                    tessellation = GetBodyTessellation(masterBody, getColor, surfaceDeviation, angleDeviation);

                    totalTessellation.Add(tessellation);
                }

                foreach (var component in part.Components) {
                    int id;
                    if(!partIDs.TryGetValue(component.Template, out id))
                        continue;

                    var threeJsComponent = new ThreeJsComponent {
                        Id = id,
                        Name=component.Template.Name,
                        Transformation = component.Placement
                    };

                    totalTessellation.Components.Add(threeJsComponent);
                }

                partTessellations.Add(totalTessellation);
            }

            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            using (JsonWriter writer = new JsonTextWriter(sw)) {
                writer.Formatting = Formatting.Indented;

                writer.WriteStartArray();

                foreach (var tessellation in partTessellations)
                    tessellation.ToJson(writer);

                writer.WriteEndArray();
            }

            var result = sb.ToString();

            var d = new SaveFileDialog();
            d.FileName = document.Path;
            d.ShowDialog();

            File.WriteAllText(d.FileName, result);
        }
Example #3
0
        public void ExportToJSON(string fileName)
        {
            var activeDoc = ((IModelDoc2)SwApp.ActiveDoc);

            var maxId   = 0;
            var partIDs = new Dictionary <Tuple <object, string, long?>, int>();

            if (activeDoc is IAssemblyDoc)
            {
                partIDs[new Tuple <object, string, long?>(activeDoc.ConfigurationManager.ActiveConfiguration.GetRootComponent3(true), activeDoc.ConfigurationManager.ActiveConfiguration.Name, null)] = maxId++;

                foreach (Component2 component in (object[])((IAssemblyDoc)activeDoc).GetComponents(true))
                {
                    AddIds(component, partIDs, ref maxId);
                }
            }
            else
            {
                partIDs[new Tuple <object, string, long?>(activeDoc, activeDoc.ConfigurationManager.ActiveConfiguration.Name, null)] = maxId++;
            }

            var modelDocList = new Tuple <object, string, long?> [maxId];

            foreach (var pair in partIDs)
            {
                modelDocList[pair.Value] = pair.Key;
            }

            var partTessellations = new List <PartTessellation>();

            foreach (var modelDoc in modelDocList)
            {
                var totalTessellation = new PartTessellation();

                var partDoc = modelDoc.Item1 as IPartDoc;

                if (partDoc != null)
                {
                    string oldConfiguration = null;
                    if (modelDoc.Item2 != ((IModelDoc2)modelDoc.Item1).ConfigurationManager.ActiveConfiguration.Name)
                    {
                        oldConfiguration = ((IModelDoc2)modelDoc.Item1).ConfigurationManager.ActiveConfiguration.Name;
                        ((IModelDoc2)modelDoc.Item1).ShowConfiguration2(modelDoc.Item2);
                    }

                    var bodies = (object[])partDoc.GetBodies2((int)swBodyType_e.swAllBodies, true);
                    if (bodies != null)
                    {
                        foreach (IBody2 body in bodies)
                        {
                            var tessellation = (ITessellation)body.GetTessellation(null);
                            tessellation.NeedEdgeFinMap             = true;
                            tessellation.NeedFaceFacetMap           = true;
                            tessellation.NeedVertexNormal           = true;
                            tessellation.NeedVertexParams           = false;
                            tessellation.ImprovedQuality            = false;
                            tessellation.CurveChordAngleTolerance   = 0.6;
                            tessellation.CurveChordTolerance        = 1;
                            tessellation.SurfacePlaneAngleTolerance = 0.6;
                            tessellation.SurfacePlaneTolerance      = 1;

                            tessellation.Tessellate();

                            var bodyTessellation = GetBodyMesh(body, partDoc, tessellation, modelDoc.Item3);

                            totalTessellation.Meshes.Add(bodyTessellation);

                            var edges = (object[])body.GetEdges();
                            if (edges != null)
                            {
                                foreach (var edge in edges)
                                {
                                    var polyline = GetEdgePolyline(tessellation, edge);
                                    totalTessellation.Lines.Add(new BodyTessellation {
                                        VertexPositions = new List <double[]>(polyline.Select(v => (double[])tessellation.GetVertexPoint(v)))
                                    });
                                }
                            }
                        }
                    }

                    if (oldConfiguration != null)
                    {
                        ((IModelDoc2)modelDoc.Item1).ShowConfiguration2(oldConfiguration);
                    }
                }

                var assemblyDoc = modelDoc.Item1 as IComponent2;

                if (assemblyDoc != null)
                {
                    foreach (Component2 component in (object[])assemblyDoc.GetChildren())
                    {
                        if (component.IsHidden(true))
                        {
                            continue;
                        }

                        var tuple = GetTuple(component);

                        int id;
                        if (!partIDs.TryGetValue(tuple, out id))
                        {
                            continue;
                        }

                        var transformation = component.Transform2;

                        var assemblyTransform = assemblyDoc.GetTotalTransform(true);
                        if (assemblyTransform != null)
                        {
                            transformation = (MathTransform)transformation.Multiply(assemblyTransform.Inverse());
                        }

                        var threeJsComponent = new ThreeJsComponent {
                            Id             = id,
                            Transformation = transformation
                        };

                        totalTessellation.Components.Add(threeJsComponent);
                    }
                }

                partTessellations.Add(totalTessellation);
            }

            StringBuilder sb = new StringBuilder();
            StringWriter  sw = new StringWriter(sb);

            using (JsonWriter writer = new JsonTextWriter(sw)) {
                writer.Formatting = Formatting.Indented;

                writer.WriteStartArray();

                foreach (var tessellation in partTessellations)
                {
                    tessellation.ToJson(writer);
                }

                writer.WriteEndArray();
            }

            var result = sb.ToString();

            // Strip the trailing 'w' or 'r' and any leading and trailing white space
            fileName = fileName.Substring(0, fileName.Length - 1).Trim(' ');

            // The filename contains the full path followed by just the filename. We need to strip this out.
            var searchEnd = fileName.Length - 1;

            while (true)
            {
                var spaceIndex = fileName.LastIndexOf(' ', searchEnd, searchEnd + 1);
                if (spaceIndex == -1)
                {
                    break;
                }
                if (fileName.Substring(spaceIndex + 1).ToLowerInvariant() ==
                    fileName.Substring(spaceIndex - (fileName.Length - spaceIndex - 1), fileName.Length - spaceIndex - 1).ToLowerInvariant())
                {
                    fileName = fileName.Substring(0, spaceIndex);
                    break;
                }
                else
                {
                    searchEnd = spaceIndex - 1;
                }
            }

            File.WriteAllText(fileName, result);
        }
 public void Add(PartTessellation other)
 {
     Lines.AddRange(other.Lines);
     Meshes.AddRange(other.Meshes);
     Components.AddRange(other.Components);
 }