Files
machineplanner/MachinePlannerExport/MachinePlannerExportPlugin/SampleAutomation.cs

148 lines
5.3 KiB
C#

/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Forge.DesignAutomation.Inventor.Utils.Helpers;
using Inventor;
namespace MachinePlannerExportPlugin
{
[ComVisible(true)]
public class SampleAutomation
{
private readonly InventorServer inventorApplication;
public SampleAutomation(InventorServer inventorApp)
{
inventorApplication = inventorApp;
}
public void Run(Document doc)
{
LogTrace("Run called with {0}", doc.DisplayName);
}
public void RunWithArguments(Document doc, NameValueMap map)
{
LogTrace("Processing " + doc.FullFileName);
try
{
// Parameter aus der Config lesen
double height = 1.0;
if (map.HasKey("height"))
{
height = map.AsDouble("height");
}
double width = 1.0;
if (map.HasKey("width"))
{
width = map.AsDouble("width");
}
string name = "Box";
if (map.HasKey("name"))
{
name = map.AsString("name");
}
// Neues Part-Dokument anlegen
PartDocument partDoc = (PartDocument)inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject,
inventorApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject), true);
PartComponentDefinition compDef = partDoc.ComponentDefinition;
compDef.Document.Name = name; // Set the document name
// 2D-Skizze auf XY-Ebene anlegen
PlanarSketch sketch = compDef.Sketches.Add(compDef.WorkPlanes[3]); // 3 = XY Plane
// Rechteck zeichnen
sketch.SketchLines.AddAsTwoPointRectangle(
inventorApplication.TransientGeometry.CreatePoint2d(0, 0),
inventorApplication.TransientGeometry.CreatePoint2d(width, height)
);
// Rechteck extrudieren (Würfel)
Profile profile = sketch.Profiles.AddForSolid();
ExtrudeDefinition extrudeDef = compDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(
profile, PartFeatureOperationEnum.kJoinOperation);
extrudeDef.SetDistanceExtent(width, PartFeatureExtentDirectionEnum.kPositiveExtentDirection);
ExtrudeFeature boxFeature = compDef.Features.ExtrudeFeatures.Add(extrudeDef);
// Speicherort für das temporäre IPT
string tempDir = System.IO.Path.GetTempPath();
string partPath = System.IO.Path.Combine(tempDir, "CubeExport.ipt");
partDoc.SaveAs(partPath, false);
// Jetzt ist FullFileName gesetzt und du kannst das Verzeichnis verwenden
string stepPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(partDoc.FullFileName), "CubeExport.step");
// STEP-Export
partDoc.SaveAs(stepPath, true); // true für STEP-Export, wenn als .step angegeben
// Optional: Log-Ausgabe
LogTrace($"Würfel mit Breite={width} und Höhe={height} und Text '{name}' als STEP exportiert: {stepPath}");
}
catch (Exception e)
{
LogError("Processing failed. " + e.ToString());
}
}
#region Logging utilities
/// <summary>
/// Log message with 'trace' log level.
/// </summary>
private static void LogTrace(string format, params object[] args)
{
Trace.TraceInformation(format, args);
}
/// <summary>
/// Log message with 'trace' log level.
/// </summary>
private static void LogTrace(string message)
{
Trace.TraceInformation(message);
}
/// <summary>
/// Log message with 'error' log level.
/// </summary>
private static void LogError(string format, params object[] args)
{
Trace.TraceError(format, args);
}
/// <summary>
/// Log message with 'error' log level.
/// </summary>
private static void LogError(string message)
{
Trace.TraceError(message);
}
#endregion
}
}