public static void CreateBlock()
{
PartDocument oDoc = (PartDocument)oApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject);
PartComponentDefinition oCD = oDoc.ComponentDefinition;
PlanarSketch sketch = oCD.Sketches.Add(oCD.WorkPlanes[3]);
TransientGeometry oTG = oApp.TransientGeometry;
SketchPoint startPoint = (SketchPoint)sketch.AddByProjectingEntity(oCD.WorkPoints[1]);
SketchLine centerLine = sketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(0, 0), oTG.CreatePoint2d(0, 5));
centerLine.StartSketchPoint.Merge(startPoint);
sketch.GeometricConstraints.AddVertical(centerLine);
SketchBlockDefinition blockDef = CreateBlockDefinition(oDoc);
sketch.SketchBlocks.AddByDefinition(blockDef, oTG.CreatePoint2d(1, 5));
oDoc.Update();
/*
и вот тут мне надо:
1. совместить точку centerLine.EndSketchPoint и точку,
которая в блоке является line1.EndSketchPoint или line3.StartSketchPoint
2. наложить горизонтальность на линию, которая в блоке является line1.
*/
}
public static SketchBlockDefinition CreateBlockDefinition(PartDocument oDoc)
{
PartComponentDefinition oCD = oDoc.ComponentDefinition;
SketchBlockDefinition blockDef = oCD.SketchBlockDefinitions.Add("MyBlock");
Inventor.Application oApp = (Inventor.Application)oCD.Application;
TransientGeometry oTG = oApp.TransientGeometry;
var startPoint = blockDef.InsertionPoint.Geometry;
int length = 1;
var line1 = blockDef.SketchLines.AddByTwoPoints(startPoint, oTG.CreatePoint2d(startPoint.X + length, startPoint.Y));
var line2 = blockDef.SketchLines.AddByTwoPoints(startPoint, oTG.CreatePoint2d(startPoint.X, startPoint.Y + length));
var line3 = blockDef.SketchLines.AddByTwoPoints(line1.EndSketchPoint, line2.EndSketchPoint);
blockDef.GeometricConstraints.AddGround((SketchEntity)blockDef.InsertionPoint);
blockDef.GeometricConstraints.AddCoincident((SketchEntity)line1.StartSketchPoint, (SketchEntity)blockDef.InsertionPoint);
blockDef.IsInsertionPointVisible = true;
/*
вот тут кстати нехилая "бажина", если сделать не AddCoincident, а
blockDef.InsertionPoint.Merge(line1.StartSketchPoint)
то вставка в эскиз .SketchBlocks.AddByDefinition() работать не будет, а
попытка обратиться к blockDef.IsInsertionPointVisible выбрасывает исключение;
походу InsertionPoint повреждается каким-то образом
*/
line1.StartSketchPoint.Merge(line2.StartSketchPoint);
blockDef.GeometricConstraints.AddHorizontal(line1);
blockDef.GeometricConstraints.AddVertical(line2);
double offset = 0.5;
blockDef.DimensionConstraints.AddTwoPointDistance
(
line1.StartSketchPoint,
line1.EndSketchPoint,
DimensionOrientationEnum.kAlignedDim,
oTG.CreatePoint2d(line1.Geometry.MidPoint.X, line1.Geometry.MidPoint.Y - offset)
);
blockDef.DimensionConstraints.AddTwoPointDistance
(
line2.StartSketchPoint,
line2.EndSketchPoint,
DimensionOrientationEnum.kAlignedDim,
oTG.CreatePoint2d(line2.Geometry.MidPoint.X - offset, line2.Geometry.MidPoint.Y)
);
return blockDef;
}