davca-toolkit/learning/solidworks/readpart/Program.cs @ 6c5a164a
| 6c5a164a | wcadmin | using System;
|
|
using System.IO;
|
|||
using SolidWorks.Interop.sldworks;
|
|||
using SolidWorks.Interop.swconst;
|
|||
namespace ReadPartApp
|
|||
{
|
|||
class Program
|
|||
{
|
|||
static void Main(string[] args)
|
|||
{
|
|||
SldWorks? swApp = null;
|
|||
try
|
|||
{
|
|||
Console.WriteLine("Connecting to SolidWorks...");
|
|||
swApp = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")!) as SldWorks;
|
|||
if (swApp == null)
|
|||
{
|
|||
Console.WriteLine("Could not connect to SolidWorks.");
|
|||
return;
|
|||
}
|
|||
string partPath = Path.Combine(Directory.GetCurrentDirectory(), "assets", "Teil1.SLDPRT");
|
|||
if (!File.Exists(partPath))
|
|||
{
|
|||
Console.WriteLine($"File not found: {partPath}");
|
|||
return;
|
|||
}
|
|||
Console.WriteLine($"Opening part: {partPath}");
|
|||
int errors = 0;
|
|||
int warnings = 0;
|
|||
ModelDoc2 swModel = swApp.OpenDoc6(partPath, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
|
|||
if (swModel == null)
|
|||
{
|
|||
Console.WriteLine($"Failed to open document. Errors: {errors}");
|
|||
return;
|
|||
}
|
|||
PrintModelInfo(swModel);
|
|||
swApp.CloseDoc(partPath);
|
|||
}
|
|||
catch (Exception ex)
|
|||
{
|
|||
Console.WriteLine($"An error occurred: {ex.Message}");
|
|||
}
|
|||
finally
|
|||
{
|
|||
swApp = null;
|
|||
GC.Collect();
|
|||
GC.WaitForPendingFinalizers();
|
|||
}
|
|||
}
|
|||
static void PrintModelInfo(ModelDoc2 swModel)
|
|||
{
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Model Information & Summary Metadata ---");
|
|||
Console.WriteLine("===========================================");
|
|||
Console.WriteLine($"Title: {swModel.GetTitle()}");
|
|||
Console.WriteLine($"Path: {swModel.GetPathName()}");
|
|||
Console.WriteLine($"Type: {(swDocumentTypes_e)swModel.GetType()}");
|
|||
Console.WriteLine($"Author: {swModel.SummaryInfo[1]}");
|
|||
Console.WriteLine($"Keywords: {swModel.SummaryInfo[2]}");
|
|||
Console.WriteLine($"Comments: {swModel.SummaryInfo[3]}");
|
|||
Console.WriteLine($"Title (Metadata): {swModel.SummaryInfo[4]}");
|
|||
Console.WriteLine($"Subject: {swModel.SummaryInfo[5]}");
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Mass Properties ---");
|
|||
Console.WriteLine("===========================================");
|
|||
MassProperty swMassProp = (MassProperty)swModel.Extension.CreateMassProperty();
|
|||
if (swMassProp != null)
|
|||
{
|
|||
// Note: Values are typically returned in system base units (meters, kilograms)
|
|||
Console.WriteLine($"Mass: {swMassProp.Mass} kg");
|
|||
Console.WriteLine($"Volume: {swMassProp.Volume} m^3");
|
|||
Console.WriteLine($"Surface Area: {swMassProp.SurfaceArea} m^2");
|
|||
double[] com = (double[])swMassProp.CenterOfMass;
|
|||
if (com != null && com.Length >= 3)
|
|||
{
|
|||
Console.WriteLine($"Center of Mass: ({com[0]:F5}, {com[1]:F5}, {com[2]:F5}) m");
|
|||
}
|
|||
}
|
|||
if (swModel is PartDoc swPart)
|
|||
{
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Material Information ---");
|
|||
Console.WriteLine("===========================================");
|
|||
string dbName;
|
|||
// Get material of the active configuration
|
|||
Configuration activeConfig = (Configuration)swModel.GetActiveConfiguration();
|
|||
string configName = activeConfig != null ? activeConfig.Name : "Default";
|
|||
string materialName = swPart.GetMaterialPropertyName2(configName, out dbName);
|
|||
if (string.IsNullOrEmpty(materialName))
|
|||
{
|
|||
Console.WriteLine("Material: <Not specified>");
|
|||
}
|
|||
else
|
|||
{
|
|||
Console.WriteLine($"Material: {materialName} (Database: {dbName})");
|
|||
}
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Bodies ---");
|
|||
Console.WriteLine("===========================================");
|
|||
object[] bodies = (object[])swPart.GetBodies2((int)swBodyType_e.swAllBodies, true);
|
|||
if (bodies != null && bodies.Length > 0)
|
|||
{
|
|||
foreach (Body2 body in bodies)
|
|||
{
|
|||
Console.WriteLine($"Body: {body.Name} (Type: {(swBodyType_e)body.GetType()}, Faces count: {body.GetFaceCount()})");
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
Console.WriteLine("No bodies found.");
|
|||
}
|
|||
}
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Configurations & Custom Properties ---");
|
|||
Console.WriteLine("===========================================");
|
|||
string[] configNames = (string[])swModel.GetConfigurationNames();
|
|||
if (configNames != null)
|
|||
{
|
|||
PrintProperties(swModel, ""); // Document level
|
|||
foreach (string config in configNames)
|
|||
{
|
|||
PrintProperties(swModel, config); // Configuration level
|
|||
}
|
|||
}
|
|||
Console.WriteLine("\n===========================================");
|
|||
Console.WriteLine("--- Features Tree (Hierarchical Structure) ---");
|
|||
Console.WriteLine("===========================================");
|
|||
Feature swFeat = (Feature)swModel.FirstFeature();
|
|||
PrintFeatures(swFeat, 0);
|
|||
}
|
|||
static void PrintProperties(ModelDoc2 swModel, string configName)
|
|||
{
|
|||
string header = string.IsNullOrEmpty(configName) ? "Document Level Properties" : $"Configuration: {configName}";
|
|||
Console.WriteLine($"\n [{header}]");
|
|||
CustomPropertyManager swCustPropMgr = swModel.Extension.get_CustomPropertyManager(configName);
|
|||
if (swCustPropMgr == null) return;
|
|||
string[] propNames = (string[])swCustPropMgr.GetNames();
|
|||
if (propNames != null && propNames.Length > 0)
|
|||
{
|
|||
foreach (string propName in propNames)
|
|||
{
|
|||
string valOut;
|
|||
string resValOut;
|
|||
bool resolved;
|
|||
swCustPropMgr.Get6(propName, false, out valOut, out resValOut, out resolved, out _);
|
|||
Console.WriteLine($" {propName}: {resValOut} (Raw formula: {valOut})");
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
Console.WriteLine(" <No custom properties>");
|
|||
}
|
|||
}
|
|||
static void PrintFeatures(Feature swFeat, int depth)
|
|||
{
|
|||
while (swFeat != null)
|
|||
{
|
|||
string indent = new string(' ', depth * 3);
|
|||
Console.WriteLine($"{indent}|- Feature: {swFeat.Name} [Type: {swFeat.GetTypeName2()}]");
|
|||
// Recursively get sub-features (e.g. sketches inside extrudes, equations inside folders)
|
|||
Feature swSubFeat = (Feature)swFeat.GetFirstSubFeature();
|
|||
if (swSubFeat != null)
|
|||
{
|
|||
PrintFeatures(swSubFeat, depth + 1);
|
|||
}
|
|||
swFeat = (Feature)swFeat.GetNextFeature();
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|