Hi All,
I am trying to program a hole on a 3D object, but without any luck. I am following the example from here
2018 SOLIDWORKS API Help - Create Hole Wizard Feature Data Object and Hole Wizard Feature Example (VBA)
Here is my code in C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace swApiPartDT
{
class PartModel
{
public string PartName { get; set; }
public double Dim_1 { get; set; }
public double Dim_2 { get; set; }
public double Dim_A { get; set; }
public double Dim_B { get; set; }
public double Dim_C { get; set; }
public double Dim_D { get; set; }
SldWorks swApp;
ModelDoc2 swModel;
Feature swFeature;
WizardHoleFeatureData2 swWzdHole;
bool status;
string defaultPartTemplate;
public void CreatePart()
{
// Define the unique name or id to store the file
string guid = Guid.NewGuid().ToString();
string root = @"C:\\" + guid;
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
else
{
MessageBox.Show("a file with the same name exists. . .");
}
// Open Solidworks application
swApp = GetSolidWorks.GetApplication();
// Choose the template for using Solidworks
defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
// Create new document with the choosen template
swApp.NewDocument(defaultPartTemplate, 0, 0, 0);
swModel = (ModelDoc2)swApp.ActiveDoc;
swFeature = swModel.FeatureByPositionReverse(3); // Get the handle to the feature by position reversed id number 2
swFeature.Name = "RefPlane";
status = swModel.Extension.SelectByID2("RefPlane", "PLANE", 0, 0, 0, false, 0, null, 0);
swModel.InsertSketch2(true);
swModel.SketchRectangle(-Dim_B / 2, Dim_C, 0, -(Dim_B - Dim_A) / 2, 0,0,false);
swModel.CreateCenterLineVB(0, 0, 0, 0, 50, 0);
swFeature = swModel.FeatureByPositionReverse(0);
swFeature.Name = "Sketch1";
status = swModel.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
status = swModel.Extension.SelectByID2("Axis1", "AXIS", 0, 0, 0, true, 16, null, (int)swSelectOption_e.swSelectOptionDefault);
swModel.FeatureManager.FeatureRevolve2(true, true, false, false, true, false, 0, 0, 3.14, 0, false, false, 0, 0, 0, 0, 0, true, true, true);
// Up to this point, the 3D model is succesfully created
/* Create a hole wizard -> hole is not created*/
object swFeatDataObj = swModel.FeatureManager.CreateDefinition((int)swFeatureNameID_e.swFmHoleWzd);
swWzdHole = (WizardHoleFeatureData2)swFeatDataObj;
swWzdHole.InitializeHole((int)swWzdGeneralHoleTypes_e.swWzdHole, (int)swWzdHoleStandards_e.swStandardAnsiMetric, (int)swWzdHoleStandardFastenerTypes_e.swStandardAnsiInchDowelHole, "M5", (int)swEndConditions_e.swEndCondThroughAll);
status = swModel.Extension.SelectByID2("", "FACE", (Dim_B)-((Dim_B - Dim_A) / 2), Dim_C / 2, 0, false, 0, null, 0);
swModel.FeatureManager.CreateFeature(swWzdHole);
swModel.ViewZoomtofit2();
swModel.ForceRebuild3(true);
swModel.SaveAs3(root.ToString() + "\\" + PartName + ".sldprt", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)512);
}
}
}
No errors were produced from this code. Only that it shows the plane (the surface of the 3D model) where the hole is supposed to be created get highlighted.
I wonder if anyone can help in pointing out the problem?
Thanks