/* Straight forward method, opens the doc in the way we want it, debugs any errors */ //Returns null on failure static SldWorks.ModelDoc2 OpenDoc(SldWorks.SldWorks app, string filePath) { int errors = 0; int warnings = 0; Console.WriteLine("Opening Doc: " + filePath); SldWorks.ModelDoc2 doc = app.OpenDoc6( filePath, (int)(SwConst.swDocumentTypes_e.swDocASSEMBLY), (int)(SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent | SwConst.swOpenDocOptions_e.swOpenDocOptions_ReadOnly), "default", ref errors, ref warnings); if (errors != 0) { Console.WriteLine("Error: " + errors); } if (warnings != 0) { Console.WriteLine("Warnings: " + warnings); } return(doc); }
static void Main(string[] args) { if (args.Length != 2 || args[0] == "-h") { PrintUsage(); return; } List <string> filesToParse = new List <string>(); List <string> meshTitles = new List <string>(); if (!ParseIndexFile(args[0], ref filesToParse, ref meshTitles)) { Console.WriteLine("Couldn't read index file"); return; } if (filesToParse.Count != meshTitles.Count) { Console.WriteLine("Something amiss: Had different number of files than mesh names. Maybe the index file specified is malformed?"); Console.WriteLine("Num Files: " + filesToParse.Count + ", Num Mesh Names: " + meshTitles.Count); return; } SldWorks.SldWorks swApp = new SldWorks.SldWorks(); List <string> xmlTags = new List <string>(); try { for (int i = 0; i < filesToParse.Count; i++) { SldWorks.ModelDoc2 doc = OpenDoc(swApp, filesToParse[i]); if (doc == null) { Console.WriteLine("Null Doc: " + filesToParse[i]); Console.WriteLine("Check that the index file specified is properly formed."); } else { MassPropertySet mpSet = GetMassPropertiesFromDoc(doc); xmlTags.AddRange(GenerateXMLTags(meshTitles[i], mpSet)); /* It makes the most sense to close the solidworks document at this point... * but for some reason that was giving errors... maybe something about lazy loading? * So instead we leave them all open until we are done with them */ // doc.Close(); } } } catch { Console.WriteLine("Problem while reading from solidworks files"); } swApp.ExitApp(); swApp = null; Console.WriteLine("Outputting to xacro"); OutputToXacroFile(args[1], xmlTags); }
static MassPropertySet GetMassPropertiesFromDoc(SldWorks.ModelDoc2 doc) { /* Open the model doc extension object * I think they just made this so that they didnt have to change the original model object interface? * Their API is a piece of work... */ SldWorks.ModelDocExtension docExtension = doc.Extension; /* One option is to use the GetMassProperties function, * but this will only return mass properties about the centre of mass, aligned with the assemblies default coordinate system * see: http://help.solidworks.com/2015/English/api/sldworksapi/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.IModelDocExtension~GetMassProperties.html */ /* Instead, we probably want to create our own MassProperties object, * so that we can get mass properties with respect to a coordinate system * of our choosing. * see: http://help.solidworks.com/2015/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~CreateMassProperty.html */ SldWorks.MassProperty massProperty = docExtension.CreateMassProperty(); //Note: we don't call AddBodies or IAddBodies, so that all bodies are calculated by default //Note: we must set the coordinate system const string coordSystemName = "urdf_coordinate_system"; SldWorks.MathTransform coordinateSystemTransform = docExtension.GetCoordinateSystemTransformByName(coordSystemName); MassPropertySet result = null; //Try to fill result if (coordinateSystemTransform == null) { Console.WriteLine("Coordinate System Transform was null."); Console.WriteLine("Coordinate System with name: '" + coordSystemName + "' was not found."); } else { massProperty.SetCoordinateSystem(coordinateSystemTransform); double[] centreOfMass = massProperty.CenterOfMass; double mass = massProperty.Mass; double[] momentsOfInertia = massProperty.GetMomentOfInertia((int)SwConst.swMassPropertyMoment_e.swMassPropertyMomentAboutCoordSys); result = new MassPropertySet(centreOfMass, mass, momentsOfInertia); } return(result); }