97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using CodeStack.SwEx.AddIn;
|
|
using CodeStack.SwEx.AddIn.Attributes;
|
|
using CodeStack.SwEx.AddIn.Enums;
|
|
using CodeStack.SwEx.Common.Attributes;
|
|
using SolidWorks.Interop.sldworks;
|
|
using SolidWorks.Interop.swconst;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace CodeStack.Examples.CreateGeometryAddIn
|
|
{
|
|
[Title("Create Geometry")]
|
|
public enum Commans_e
|
|
{
|
|
[CommandItemInfo(swWorkspaceTypes_e.Part)]
|
|
[Title("Create Cylinder")]
|
|
CreateCylinder
|
|
}
|
|
|
|
[ComVisible(true)]
|
|
public interface IGeometryAddIn
|
|
{
|
|
IFeature CreateCylinder(double diam, double height);
|
|
}
|
|
|
|
[AutoRegister("CreateGeometryAddIn", "Sample add-in for creating geometry", true)]
|
|
[ComVisible(true), Guid("799A191E-A4CF-4622-9E77-EA1A9EF07621")]
|
|
[ProgId("CodeStack.GeometryAddIn")]
|
|
public class AddIn : SwAddInEx, IGeometryAddIn
|
|
{
|
|
public override bool OnConnect()
|
|
{
|
|
this.AddCommandGroup<Commans_e>(OnButtonClick);
|
|
|
|
return true;
|
|
}
|
|
|
|
private void OnButtonClick(Commans_e cmd)
|
|
{
|
|
try
|
|
{
|
|
switch (cmd)
|
|
{
|
|
case Commans_e.CreateCylinder:
|
|
CreateCylinder(0.1, 0.1);
|
|
break;
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Trace.WriteLine(ex.Message);
|
|
App.SendMsgToUser2("Failed to create geometry",
|
|
(int)swMessageBoxIcon_e.swMbStop, (int)swMessageBoxBtn_e.swMbOk);
|
|
}
|
|
}
|
|
|
|
public IFeature CreateCylinder(double diam, double height)
|
|
{
|
|
var part = App.ActiveDoc as IPartDoc;
|
|
|
|
if (part == null)
|
|
{
|
|
throw new NotSupportedException("Only part document are supported");
|
|
}
|
|
|
|
var modeler = App.IGetModeler();
|
|
|
|
var body = modeler.CreateBodyFromCyl(new double[]
|
|
{
|
|
0, 0, 0,
|
|
0, 1, 0,
|
|
diam / 2, height
|
|
});
|
|
|
|
if (body != null)
|
|
{
|
|
var feat = part.CreateFeatureFromBody3(body, false,
|
|
(int)swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify) as IFeature;
|
|
|
|
if (feat != null)
|
|
{
|
|
return feat;
|
|
}
|
|
else
|
|
{
|
|
throw new NullReferenceException("Failed to create feature from body");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new NullReferenceException("Failed to create body. Make sure that the parameters are valid");
|
|
}
|
|
}
|
|
}
|
|
}
|