Exemplo n.º 1
0
        /// <summary>
        /// Exports the robot
        /// </summary>
        /// <param name="log"> Logger to be used to write messeges </param>
        /// <param name="path"> File path to export robot to </param>
        public override void Export(ProgressLogger log, String path)
        {
            this.log = log;
            path += "\\" + robot.Name +"_Exported";
            folderPath = path + "\\" + robot.Name;
            tempFilePath = folderPath +  "\\";
            visualSTLPath = folderPath + "\\visualSTL\\";
            collisionSTLPath = folderPath + "\\collisionSTL\\";
            if (Directory.Exists(path))
                Directory.Delete(path, true);
            Directory.CreateDirectory(path);
            Directory.CreateDirectory(folderPath);
            Directory.CreateDirectory(visualSTLPath);
            Directory.CreateDirectory(collisionSTLPath);
            try
            {
                log.WriteMessage("Setting up exporter", false);

                //Export STLs
                log.WriteMessage("Starting STL exports", false);
                ModelDoc2 ActiveDoc = (ModelDoc2)asm;
                //switchs to the visual configuration and hides all components
                Configuration currentConfig = ActiveDoc.ConfigurationManager.ActiveConfiguration;
                ActiveDoc.ShowConfiguration2(robot.VisualConfig);
                STLExporter meshMaker = new STLExporter(iSwApp, asm);
                Component2[] hiddenComps = meshMaker.HideAllComponents();
                //exports each Link's visual models
                foreach (Link L in robot.GetLinksAsArray())
                {
                    if (!robot.ContinueExport)
                    {
                        meshMaker.UnhideComponents(hiddenComps);
                        meshMaker.close();
                        ActiveDoc.ShowConfiguration2(currentConfig.Name);
                        return;
                    }
                    meshMaker.ExportLink(L, robot.VisualConfig, visualSTLPath + L.Name + ".stl", log);

                }
                //unhides all components, switches to collision configuration, then hides all components
                meshMaker.UnhideComponents(hiddenComps);
                ActiveDoc.ShowConfiguration2(robot.CollisionConfig);
                hiddenComps = meshMaker.HideAllComponents();
                //exports each Link's collision model
                foreach (Link L in robot.GetLinksAsArray())
                {
                    if (!robot.ContinueExport)
                    {
                        meshMaker.UnhideComponents(hiddenComps);
                        meshMaker.close();
                        ActiveDoc.ShowConfiguration2(currentConfig.Name);
                        return;
                    }
                    meshMaker.ExportLink(L, robot.CollisionConfig, collisionSTLPath + L.Name + ".stl", log);
                }
                //restores the model to its original configuration
                meshMaker.UnhideComponents(hiddenComps);
                ActiveDoc.ShowConfiguration(currentConfig.Name);
                meshMaker.close();
                log.WriteMessage("Finished exporting STL files", false);

                //Make URDF
                log.WriteMessage("Creating URDF file", false);
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                settings.Indent = true;
                settings.NewLineOnAttributes = false;
                XmlWriter URDFwriter = XmlWriter.Create(tempFilePath+robot.Name+".URDF", settings);
                URDFwriter.WriteStartDocument();
                WriteURDF(URDFwriter);
                URDFwriter.WriteEndDocument();
                URDFwriter.Close();
                log.WriteMessage("URDF file complete");
                CreateConfigFile();
                log.WriteMessage("Config file complete");
                log.WriteMessage("Export Complete");
            }
            catch (Exception e)
            {
                log.WriteError("Unhandled Exception thrown when exporting robot: "+e.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Exports the robot
        /// </summary>
        /// <param name="log"> Logger to be used to write messeges </param>
        /// <param name="path"> File path to export robot to </param>
        public override void Export(ProgressLogger log, String path)
        {
            this.log = log;
            robotName = robot.Name.Replace(" ", "_");

            try
            {
                if (path.Equals(System.Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\wpilib\\simulation"))
                {
                    folderPath = path + "\\models\\" + robotName;
                    tempFilePath = folderPath + "\\";
                    MeshesPath = folderPath + "\\meshes\\";
                    RobotPath = folderPath + "\\robots\\";
                    if (Directory.Exists(folderPath))
                    {
                        ClearReadOnly(new DirectoryInfo(path));
                        Directory.Delete(folderPath, true);
                    }

                    Directory.CreateDirectory(folderPath);
                    Directory.CreateDirectory(MeshesPath);
                    Directory.CreateDirectory(RobotPath);
                    folderPath = path + "\\worlds";
                    if(File.Exists(folderPath+"\\"+robotName+".world"))
                        File.Delete(folderPath+"\\"+robotName+".world");
                }
                else
                {
                    path += "\\" + robotName + "_Exported";
                    folderPath = path + "\\" + robotName;
                    tempFilePath = folderPath + "\\";
                    MeshesPath = folderPath + "\\meshes\\";
                    RobotPath = folderPath + "\\robots\\";
                    if (Directory.Exists(path))
                    {
                        ClearReadOnly(new DirectoryInfo(path));
                        Directory.Delete(path, true);
                    }
                    Directory.CreateDirectory(path);
                    Directory.CreateDirectory(folderPath);
                    Directory.CreateDirectory(MeshesPath);
                    Directory.CreateDirectory(RobotPath);
                    folderPath = path;
                }

            }
            catch (IOException e)
            {
                log.WriteMessage("Failed to create directory. Another application may be accesing the file");
                return;
            }

            try
            {
                log.WriteMessage("Setting up exporter", false);

                //Export STLs
                log.WriteMessage("Starting STL exports", false);
                ModelDoc2 ActiveDoc = (ModelDoc2)asm;

                //switches to the visual configuration and hides all components
                Configuration currentConfig = ActiveDoc.ConfigurationManager.ActiveConfiguration;
                //ActiveDoc.ShowConfiguration2(robot.VisualConfig);
                STLExporter meshMaker = new STLExporter(iSwApp, asm);
                Dictionary<string, List<ModelConfiguration>> VisualModels = new Dictionary<string, List<ModelConfiguration>>();
                Dictionary<string, List<ModelConfiguration>> CollisionModels = new Dictionary<string, List<ModelConfiguration>>();
                //Stopwatch watch = new Stopwatch();
                //watch.Start();
                //sort configurations
                foreach (Link l in robot.GetLinksAsArray())
                {
                    if(!l.VisualModel.EmptyModel)
                        if (VisualModels.ContainsKey(l.VisualModel.swConfiguration))
                            VisualModels[l.VisualModel.swConfiguration].Add(l.VisualModel);
                        else
                        {
                            List<ModelConfiguration> tempList = new List<ModelConfiguration>();
                            tempList.Add(l.VisualModel);
                            VisualModels.Add(l.VisualModel.swConfiguration, tempList);
                        }

                    if(!l.CollisionModel.EmptyModel)
                        if (CollisionModels.ContainsKey(l.CollisionModel.swConfiguration))
                            CollisionModels[l.CollisionModel.swConfiguration].Add(l.CollisionModel);
                        else
                        {
                            List<ModelConfiguration> tempList = new List<ModelConfiguration>();
                            tempList.Add(l.CollisionModel);
                            CollisionModels.Add(l.CollisionModel.swConfiguration, tempList);
                        }

                }
                //Debug.WriteLine("Finished sorting configs=" + watch.ElapsedMilliseconds);
                string[] configs = VisualModels.Keys.Union(CollisionModels.Keys).ToArray();

                //Export each config
                foreach (string config in configs)
                {
                    ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = false;
                    ActiveDoc.ShowConfiguration2(config);
                    //Debug.WriteLine("switched config " + config +"=" + watch.ElapsedMilliseconds);
                    ActiveDoc.ClearSelection2(true);
                    Component2[] hiddenComps = meshMaker.HideAllComponents();
                    //Debug.WriteLine("hid components=" + watch.ElapsedMilliseconds);
                    if(VisualModels.ContainsKey(config))
                        foreach (ModelConfiguration m in VisualModels[config])
                        {
                            Application.DoEvents();
                            if (!robot.ContinueExport)
                            {
                                meshMaker.UnhideComponents(hiddenComps);
                                meshMaker.close();
                                ActiveDoc.ShowConfiguration2(currentConfig.Name);
                                ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = true;
                                return;
                            }
                            ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = false;
                            meshMaker.ExportLink(m.Owner, m, MeshesPath + m.Owner.Name.Replace(" ", "_") + ".stl", log);
                            //Debug.WriteLine("exported visual=" + watch.ElapsedMilliseconds);
                        }
                    if(CollisionModels.ContainsKey(config))
                        foreach (ModelConfiguration m in CollisionModels[config])
                        {
                            Application.DoEvents();
                            if (!robot.ContinueExport)
                            {
                                meshMaker.UnhideComponents(hiddenComps);
                                meshMaker.close();
                                ActiveDoc.ShowConfiguration2(currentConfig.Name);
                                ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = true;
                                return;
                            }
                            ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = false;
                            meshMaker.ExportLink(m.Owner, m, MeshesPath + m.Owner.Name.Replace(" ", "_") + "_col.stl", log);
                           // Debug.WriteLine("exported col=" + watch.ElapsedMilliseconds);
                        }
                    meshMaker.UnhideComponents(hiddenComps);
                    //Debug.WriteLine("unhid comps=" + watch.ElapsedMilliseconds);
                }

                ActiveDoc.ShowConfiguration(currentConfig.Name);
                meshMaker.close();
                ((ModelView)ActiveDoc.ActiveView).EnableGraphicsUpdate = true;
                log.WriteMessage("Finished exporting STL files", false);

                //Make SDF
                log.WriteMessage("Creating SDF file", false);
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                settings.Indent = true;
                settings.NewLineOnAttributes = false;
                XmlWriter SDFwriter = XmlWriter.Create(RobotPath+robotName+".sdf", settings);
                SDFwriter.WriteStartDocument();
                WriteSDF(SDFwriter);
                SDFwriter.WriteEndDocument();
                SDFwriter.Close();
                log.WriteMessage("SDF file complete");
                CreateConfigFile();
                log.WriteMessage("Config file complete");
                log.WriteMessage("Export Complete");
            }
            catch (Exception e)
            {
                log.WriteError("Unhandled Exception thrown when exporting robot: "+e.Message);
            }
        }