版权声明:版权归个人所有!转载请注明出处。 https://blog.csdn.net/sapch33/article/details/82850191
通过空间任意选取三点生成耳板实体模型,后期可以做参数化调整或者在三维空间拖曳控制点修改板的尺寸
代码如下:
//
//////////////////////////////////////////////////////////////////////////////
//
// 通过三点生成耳板
//
// 依次选取Or Cen P1
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
//////////////////////////////////////////////////////////////////////////////
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
using AstSTEELAUTOMATIONLib;
using DSCODBCCOMLib;
using System.Windows.Forms;
namespace SampleJoint
{
[ComVisible(true)]
[Guid("DFB7A86F-7E86-40F2-8E58-C16C776F7464")]
public class CreatePlate : IRule, IJointInfo
{
private Joint m_Joint = null;
private Page1 m_Page1 = null;
#region Joint_Parameters
//Sheet 1
public double m_dPlateThickness;
public IPoint3d ptStart;
public double m_dL1;
public double m_dL2;
public double m_dL3;
public double m_dR1;
public double m_dR2;
#endregion
#region Standard_Methods
/// <summary>
/// Ask user for input with the help of AstUI
/// and add the necessary entities to InputObjects of the Joint
/// </summary>
/// <param name="pAstUI"></param>
public void Query(AstUI pAstUI)
{
IPoint3d point = (IPoint3d)(new DSCGEOMCOMLib.Point3d());
point.Create(0, 0, 0);
AstObjectsArr inputObjectsArr = m_Joint.CreateObjectsArray();
eUIErrorCodes outErrCode;
// 耳板参数输入
//选取3个点
IPoint3d Or = pAstUI.AcquirePoint(false, point, true, true, 400000, out outErrCode);
IPoint3d Cen = pAstUI.AcquirePoint(false, point, true, true, 400001, out outErrCode);
IPoint3d P1 = pAstUI.AcquirePoint(false, point, true, true, 400002, out outErrCode);
//定义成用户自定义点
userDefinedPoint uspOr = m_Joint.CreateUserDefinedPoint(Or);
userDefinedPoint uspCen = m_Joint.CreateUserDefinedPoint(Cen);
userDefinedPoint uspP1 = m_Joint.CreateUserDefinedPoint(P1);
//将3个用户自定义点加入节点集中
inputObjectsArr.Add(uspOr);
inputObjectsArr.Add(uspCen);
inputObjectsArr.Add(uspP1);
m_Joint.InputObjects = inputObjectsArr;
//load default parameters for joint
loadDefaultValues(Or, Cen, P1);
}
/// <summary>
/// Save data to Joint through Filer
/// </summary>
/// <param name="pFiler"></param>
public void OutField(IFiler pFiler)
{
//MessageBox.Show("outfield");
pFiler.writeVersion(1); //Set the current rule version
pFiler.writeItem(m_dPlateThickness, "PlateThickness");
pFiler.writeItem(m_dL1, "m_dL1");
pFiler.writeItem(m_dL2, "m_dL2");
pFiler.writeItem(m_dL3, "m_dL3");
pFiler.writeItem(m_dR1, "m_dR1");
pFiler.writeItem(m_dR2, "m_dR2");
}
/// <summary>
/// Automatically called in the case of invalid feature for the rule (if a feature is used).
/// Write your own code for such an event or simply let this function do nothing.
/// </summary>
/// <param name="reserved"></param>
public void InvalidFeature(int reserved)
{
MessageBox.Show("invalidfeature");
}
/// <summary>
/// Read data from Joint through Filer
/// </summary>
/// <param name="pFiler"></param>
public void InField(IFiler pFiler)
{
//MessageBox.Show("infield");
//Must be used in order to be able to check rule version
//if changes are required in future releases
int version = pFiler.readVersion(); //Returns the current rule version.
//readItem - Returns the data found with specified name
m_dPlateThickness = (double)pFiler.readItem("PlateThickness");
m_dL1 = (double)pFiler.readItem("m_dL1");
m_dL2 = (double)pFiler.readItem("m_dL2");
m_dL3 = (double)pFiler.readItem("m_dL3");
m_dR1 = (double)pFiler.readItem("m_dR1");
m_dR2 = (double)pFiler.readItem("m_dR2");
}
/// <summary>
/// Get user pages of rule
/// </summary>
/// <param name="pagesRet"></param>
/// <param name="pPropSheetData"></param>
public void GetUserPages(RulePageArray pagesRet, PropertySheetData pPropSheetData)
{
//MessageBox.Show("getuserpages");
//Set Title(From AstCrtlDb)
pPropSheetData.SheetPrompt = 81309;
//First Page bitmap index(From AstorBitmaps)
pPropSheetData.FirstPageBitmapIndex = 60782;
pPropSheetData.ResizeOption = eGUIDimension.kStandard;
//Property Sheet 1
RulePage rulePage1 = m_Joint.CreateRulePage();
rulePage1.title = 88438; //Base plate layout
m_Page1 = new Page1(this);
rulePage1.hWnd = m_Page1.Handle.ToInt64();
pagesRet.Add(rulePage1);
}
/// <summary>
/// Should return the name of the tabular parameters table from database
/// </summary>
/// <returns></returns>
public string GetTableName()
{
return "";
}
/// <summary>
/// Must return True or False depending on license feature usage for this rule.
/// The FeatureName parameter must be filled with the license feature name, identical to the feature
/// name in the license file (if a feature is used for the rule).
/// </summary>
/// <param name="FeatureName"></param>
/// <returns></returns>
public bool GetFeatureName(ref string FeatureName)
{
FeatureName = "";
return false;
}
/// <summary>
/// Data to be exported
/// </summary>
/// <param name="pExportFiler"></param>
public void GetExportData(IRuleExportFiler pExportFiler)
{
}
/// <summary>
/// Release user pages
/// </summary>
public void FreeUserPages()
{
m_Page1.Close();
m_Page1.Dispose();
}
/// <summary>
/// CreateObjects contains the joint functionality. It uses the global variables declared in the declaration
/// section and does the main work
/// </summary>
public void CreateObjects()
{
// MessageBox.Show("createobjects");
bool bCreationStatus = true;
//declare the created objects
AstObjectsArr createdObjectsArr = m_Joint.CreateObjectsArray();
try
{
//取出节点组中的用户自定义点
userDefinedPoint uspOr = (userDefinedPoint)m_Joint.InputObjects.OfType<IAstObject>().ElementAt(0);
userDefinedPoint uspCen = (userDefinedPoint)m_Joint.InputObjects.OfType<IAstObject>().ElementAt(1);
userDefinedPoint uspP1 = (userDefinedPoint)m_Joint.InputObjects.OfType<IAstObject>().ElementAt(2);
IPoint3d Or = uspOr.point;
IPoint3d Cen = uspCen.point;
IPoint3d P1 = uspP1.point;
// 创建耳板
CreateAugPolygon(ref createdObjectsArr, P1, Cen, Or, out IPlate plate);
//创建孔(特征)
AddPlateFeatures(plate, Cen, ref createdObjectsArr);
}
catch (COMException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
bCreationStatus = false;
}
catch (System.Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
bCreationStatus = false;
}
//Set it to false will result in a joint "red box" situation. The
//objects created by the joint until the error occurred will be deleted and the old objects will be available.
m_Joint.CreationStatus = bCreationStatus;
m_Joint.CreatedObjects = createdObjectsArr;
}
/// <summary>
/// This method must be written if the new joint was designed to replace and old HRL joint and the
/// developer intends to convert the old joint to the new one.
/// The received filer is an incremental filer. Using this filer, all parameters of the old rule are accessible.
/// Also, the conversion filer provides the rule version.
/// The method must return true if the conversion was successfully.
/// </summary>
/// <param name="filer"></param>
/// <param name="OldHRLRuleName"></param>
/// <returns></returns>
public bool ConvertFromHRL(HRLConvertFiler filer, string OldHRLRuleName)
{
return false;
}
/// <summary>
/// Get or set the joint object of this rule
/// </summary>
public Joint Joint
{
set
{
m_Joint = value;
}
get
{
return m_Joint;
}
}
public string ASVersion
{
get { return "AS 2019"; }
}
public string ApplicationName
{
get { return "耳板程序"; }
}
public string CompName
{
get { return "STC"; }
}
public string Email
{
get { return "[email protected]"; }
}
public int LogoIconId
{
get { return 0; }
}
public string Name
{
get { return "Sapch"; }
}
public string VersionNumber
{
get { return "1"; }
}
#endregion
#region Private_methods
private void loadDefaultValues(IPoint3d Or, IPoint3d Cen, IPoint3d P1)
{
//耳板默认值
m_dPlateThickness = 10;
m_dL1 = Or.DistanceTo(P1);
m_dL2 = m_dL1;
m_dL3 = Or.DistanceTo(Cen);
m_dR1 = m_dL3 / 2;
m_dR2 = m_dR1 / 1.5;
}
private void setJointTransferForPlate(ref IJointTransfer jointTrans)
{
jointTrans.ClassType = eClassType.kPlateClass;
//set here all the properties which can be modified outside the joint
jointTrans.set_Attribute(eAttributeCodes.kPlateDenotation, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMaterial, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateCoating, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSinglePartNumber, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMainPartNumber, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSinglePartPrefix, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMainPartPrefix, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateAssembly, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateItemNumber, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSinglePartDetailStyle, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMainPartDetailStyle, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateNote, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSPUsedForCollisionCheck, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSPUsedForNumbering, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSPDisplayRestriction, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateSPExplicitQuantity, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMPUsedForCollisionCheck, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMPUsedForNumbering, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMPDisplayRestriction, 1);
jointTrans.set_Attribute(eAttributeCodes.kPlateMPExplicitQuantity, 1);
}
/// <summary>
/// Utils
/// </summary>
private void MovePoint(IPoint3d ptIn, IVector3d vMove, double moveDist, out IPoint3d retPoint)
{
retPoint = (IPoint3d)(new DSCGEOMCOMLib.Point3d());
retPoint.setFrom(ptIn);
IVector3d v = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
v.setFrom(vMove);
v.Normalize();
v.Multiply(moveDist);
retPoint.Add(v);
}
#endregion
#region Examples
private void CreateAugPolygon(ref AstObjectsArr createdObjectsArr, IPoint3d P1, IPoint3d Cen, IPoint3d Or, out IPlate plate)
{
//求切点
double D, A, a ;
IPoint3d P11 = (IPoint3d)(new DSCGEOMCOMLib.Point3d());
IPoint3d P22 = (IPoint3d)(new DSCGEOMCOMLib.Point3d());
IPoint3d P2 = (IPoint3d)(new DSCGEOMCOMLib.Point3d());
D = P1.DistanceTo(Cen);
a = Math.Asin(m_dR1 / D);
A = Math.Sqrt((D * D) - (m_dR1 * m_dR1));
IVector3d v12 = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
IVector3d v1o = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
IVector3d v13 = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
IVector3d vz = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
v12 = Cen.Subtract(P1);
v1o = Or.Subtract(P1);
vz = v12.CrossProduct(v1o);
v12.RotateBy(vz, -a);
MovePoint(P1, v12, A, out P11);//P11切点
//求反向角点P2
MovePoint(Or, v1o, m_dL2, out P2);
D = P2.DistanceTo(Cen);
a = Math.Asin(m_dR1 / D);
A = Math.Sqrt((D * D) - (m_dR1 * m_dR1));
v12 = Cen.Subtract(P2);
v12.RotateBy(vz, a);
MovePoint(P2, v12, A, out P22);//P22切点
//create vertex info
IVertexInfo vertexInfo = (IVertexInfo)(new DSCGEOMCOMLib.vertexInfo());
vertexInfo.CreateFromCenterAndNormal(m_dR1, Cen, vz);
//create polygon
IAugPolygon3d polygon = (IAugPolygon3d)(new DSCGEOMCOMLib.AugPolygon3d());
polygon.AppendVertex(P1);
polygon.AppendNewVertex(P11, vertexInfo, true);
polygon.AppendVertex(P22);
polygon.AppendVertex(P2);
//create plate role and joint transfer
IRole plateRole = m_Joint.CreateRole("Plate");
IJointTransfer jointTransfer = m_Joint.CreateJointTransfer("Plate");
setJointTransferForPlate(ref jointTransfer);
//create plate
plate = m_Joint.CreatePlatePoly((AstSTEELAUTOMATIONLib.Role)plateRole, polygon, m_dPlateThickness);
//Add plate to created object array
if (plate != null)
{
//set joint transfer
plate.JointTransfer = (AstSTEELAUTOMATIONLib.JointTransfer)jointTransfer;
plate.Portioning = 0;
createdObjectsArr.Add(plate);
}
// MessageBox.Show("耳板已生成!");
}
private void AddPlateFeatures(IPlate basePlate, IPoint3d Cen, ref AstObjectsArr createdObjectsArr)
{
//板上开孔(Feature)
IVector3d xAxis = (IVector3d)(new DSCGEOMCOMLib.Vector3d());
xAxis = basePlate.getCS().XAxis;
Role partRole = m_Joint.CreateRole("Feature");
IPlateFeat plateFeat = basePlate.addPlateCircleContourFeat(partRole, xAxis, Cen, m_dR2);
if (plateFeat != null)
{
createdObjectsArr.Add(plateFeat);
}
}
#endregion
}
}