Hi, I am working on C# script for API.
I have created all feature on origin. I want to select surface without using coordinate position for trimming purpose.
My question:
Suppose, I have two profile surface too far from origin position and now I want to select this two surface for trimmingAPI Macros#Surfacing
I have attached cad model 2025 version
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace SurfaceSelectHelper
{
class Program
{
// This event will be set by the external event handler when trim finishes
// (Do NOT add the handler here if you said not to — handler must call Program.trimmedEvent.Set();)
public static ManualResetEventSlim trimmedEvent = new ManualResetEventSlim(false);
[STAThread]
static void Main()
{
// Attach to running SolidWorks
SldWorks swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
// Active part document
ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
// ---- Surface 1 (Right Plane) ----
// Select Right plane
swModel.Extension.SelectByID2("Right plane", "PLANE", 0, 0, 0, false, 0, null, 0);
// Insert sketch
swModel.SketchManager.InsertSketch(true);
// Create a line 01
swModel.SketchManager.CreateLine(-0.068922, 0.023964, 0.0, 0.042733, 0.005543, 0.0);
// Exit sketch
swModel.SketchManager.InsertSketch(true);
// Show Isometric view
swModel.ShowNamedView2("*Isometric", (int)swStandardViews_e.swIsometricView);
// Zoom to fit
swModel.ViewZoomtofit2();
// Extrude surface 01
swModel.FeatureManager.FeatureExtruRefSurface3(
true, // Sd: single-ended
false, // Dir: flip direction (reverse)
(int)swStartConditions_e.swStartSketchPlane, // StartCond
0, // OffsetVal
(int)swEndConditions_e.swEndCondBlind, // T1 (termination type for end 1)
(int)swEndConditions_e.swEndCondBlind, // T2 (not used when Sd=true)
0.06604, // D1 -> depth (meters) for the (flipped) single end
0, // D2 (unused for single-ended)
true, false, false, false,
0, 0, false, false, false, false, false, false,
false, false);
// Clear previous selections
swModel.ClearSelection2(true);
// ---- Surface 2 (Right Plane) ----
// Select Right plane
swModel.Extension.SelectByID2("Right plane", "PLANE", 0, 0, 0, false, 0, null, 0);
// Insert sketch
swModel.SketchManager.InsertSketch(true);
// Create a line 02
swModel.SketchManager.CreateLine(0.0, 0.023059, 0.0, 0.0, -0.081662, 0.0);
// Exit sketch
swModel.SketchManager.InsertSketch(true);
// --- WAIT here for external event-handler to signal trim finished ---
// Wait up to 10 seconds (10000 ms). No if/else used; we capture the boolean result.
bool signaled = trimmedEvent.Wait(10000);
// Extrude surface 02
swModel.FeatureManager.FeatureExtruRefSurface3(
false, // Sd: single-ended
false, // Dir: flip direction (reverse)
(int)swStartConditions_e.swStartSketchPlane, // StartCond
0, // OffsetVal
(int)swEndConditions_e.swEndCondBlind, // T1 (termination type for end 1)
(int)swEndConditions_e.swEndCondBlind, // T2 (not used when Sd=true)
0.06604, // D1 -> depth (meters) for the (flipped) single end
0.02, // D2 (unused for single-ended)
true, false, false, false,
0, 0, false, false, false, false, false, false,
false, false);
swModel.ClearSelection2(true);
swModel.EditRebuild3();
// Select second surface
bool selByName = swModel.Extension.SelectByID2("Surface-Extrude2", "FACEBODY", 0, 0, 0, true, 0, null, 0);
// --- Now do PreTrimSurface, select the target surface body and PostTrimSurface ---
// Call PreTrimSurface (same boolean order as your VB sample)
var preTrimStatus = swModel.FeatureManager.PreTrimSurface(false, true, false, true);
// Select the surface body by coordinates(you provided these coordinates)
// Note: use exact double values (scientific notation allowed)
bool selResult = swModel.Extension.SelectByID2(
"", "FACEBODY", 4.76514312435938E-02, 8.84694633197114E-03, -2.27068272785118E-02, true, 0, null, 0);
// Call PostTrimSurface and capture returned Feature object
object myFeature = swModel.FeatureManager.PostTrimSurface(true);
// Clear selection and rebuild to update model
swModel.ClearSelection2(true);
swModel.EditRebuild3();
}
}
}
