//設定選取過濾條件為只允許sheet body跟face
private int init_proc(IntPtr select, IntPtr user_data)
{
UFUi.Mask[] masks = {
new UFUi.Mask {object_type = UFConstants.UF_solid_type, object_subtype = 0, solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_SHEET},
new UFUi.Mask {object_type = UFConstants.UF_solid_type, object_subtype = 0, solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE}
};
try
{
theUfSession.Ui.SetSelMask(select, UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, 2, masks);
return UFConstants.UF_UI_SEL_SUCCESS;
}
catch
{
return UFConstants.UF_UI_SEL_FAILURE;
}
}
//選擇多個sheet body或是face
private int SelectSheetsOrFaces(string prompt, out Tag[] objects)
{
int resp;
int count = 0;
double[] cursor = new double[3];
objects = null;
//選取多個物件
theUfSession.Ui.SelectWithClassDialog ("Select faces or sheets", prompt, UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY,
init_proc, IntPtr.Zero, out resp, out count, out objects);
if (resp == UFConstants.UF_UI_BACK || resp == UFConstants.UF_UI_CANCEL)
return 0;
for (int i = 0; i < count; i++)
theUfSession.Disp.SetHighlight (objects[i], 0);
return count;
}
//使用原點及邊長的方式建立方塊
public Block CreateBlock(double[] origin, string[] lengths)
{
Part workPart = theSession.Parts.Work;
BlockFeatureBuilder blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(null);
blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;
blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths;
//根據錄製的Journal內容完成blockFeatureBuilder1的設定
//your code here
NXOpen.Point3d originPt = new NXOpen.Point3d(origin[0],origin[1],origin[2]);
blockFeatureBuilder1.SetOriginAndLengths(originPt,lengths[0],lengths[1],lengths[2]);
Block feature1 = (Block)blockFeatureBuilder1.CommitFeature();
blockFeatureBuilder1.Destroy();
return feature1;
}
private Body [] CreateWrapBlock(Tag[] objects)
{
Body [] result = null;
double[] min = {Double.MaxValue, 99999999, 99999999};
double[] max = {-99999999, -99999999, -99999999};
//依序計算每個物件的最小方盒, 並計算總體的最小方盒 (UF_MODL)
for (int i = 0; i < objects.Length; i++)
{
double[] bbox = new double[6];
theUfSession.Modl.AskBoundingBox(objects[i], bbox );
min[0] = (min[0] < bbox[0]) ? min[0] : bbox[0];
min[1] = (min[1] < bbox[1]) ? min[1] : bbox[1];
min[2] = (min[2] < bbox[2]) ? min[2] : bbox[2];
max[0] = (max[0] > bbox[3]) ? max[0] : bbox[3];
max[1] = (max[1] > bbox[4]) ? max[1] : bbox[4];
max[2] = (max[2] > bbox[5]) ? max[2] : bbox[5];
}
//取得最小方盒三個方向長度的字串表示
string xLen = (max[0] - min[0]).ToString();
string yLen = (max[1] - min[1]).ToString();
string zLen = (max[2] - min[2]).ToString();
string[] edgeLen = {xLen, yLen, zLen};
//建立block特徵
Block block = CreateBlock(min, edgeLen);
if (block != null)
{
//取得block特徵底下的body
result = block.GetBodies();
}
return result;
}