-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
165 lines (146 loc) · 8.09 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inventor;
namespace InventorBasics
{
public partial class Form1 : Form
{
Inventor.Application inventor = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
try
{
inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application; ;
}
catch (Exception)
{
Type inventorType = System.Type.GetTypeFromProgID("Inventor.Application");
inventor = System.Activator.CreateInstance(inventorType) as Inventor.Application;
inventor.Visible = true;
}
}
catch (Exception)
{
MessageBox.Show("Error connecting to inventor", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (inventor == null || string.IsNullOrWhiteSpace(CaptionTB.Text))
{
if (inventor == null)
MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (inventor.ActiveView != null)
inventor.ActiveView.Caption = CaptionTB.Text;
}
private void button2_Click(object sender, EventArgs e)
{
int width, height;
if (!int.TryParse(widthTB.Text, out width) || !int.TryParse(HeightTB.Text, out height) || inventor == null)
{
if (inventor == null)
MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (inventor.ActiveView != null)
{
inventor.ActiveView.Width = width;
inventor.ActiveView.Height = height;
}
}
private void CreatePart_Click(object sender, EventArgs e)
{
if (inventor == null)
{
MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
PartDocument doc = inventor.Documents.Add(DocumentTypeEnum.kPartDocumentObject, null , true) as PartDocument;
doc.PropertySets["{F29F85E0-4FF9-1068-AB91-08002B27B3D9}"]["Author"].Value = "Vladyslav Romanchuk";
//User-defined property
doc.PropertySets["{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"].Add("Parts R Us", "Supplier");
PartComponentDefinition partDefinition = (PartComponentDefinition)doc.ComponentDefinition;
// Create a 2D sketch on the X-Y plane.
PlanarSketch sketch1 = (PlanarSketch)partDefinition.Sketches.Add(partDefinition.WorkPlanes[3]);
TransientGeometry tg = inventor.TransientGeometry;
Point2d[] points = new Point2d[5] { tg.CreatePoint2d(0, 0), tg.CreatePoint2d(0, 20), tg.CreatePoint2d(20, 20), tg.CreatePoint2d(20, -10), tg.CreatePoint2d(10, -10) };
SketchLine[] lines = new SketchLine[5];
lines[0] = sketch1.SketchLines.AddByTwoPoints(points[0], points[1]);
for (int i = 1; i < lines.Length - 1; i++)
lines[i] = sketch1.SketchLines.AddByTwoPoints(lines[i-1].EndSketchPoint, points[i + 1]);
sketch1.SketchArcs.AddByCenterStartEndPoint(tg.CreatePoint2d(10, 0), lines[3].EndSketchPoint, lines[0].StartSketchPoint, false);
//Extrude
Profile profile = sketch1.Profiles.AddForSolid();
ExtrudeDefinition extrudeDefinition = partDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kNewBodyOperation);
extrudeDefinition.SetDistanceExtent(6, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection);
ExtrudeFeature extrude = (ExtrudeFeature)partDefinition.Features.ExtrudeFeatures.Add(extrudeDefinition);
//second scatch
Face topCap = extrude.EndFaces[1];
sketch1 = partDefinition.Sketches.Add(topCap, false);
Point2d center = sketch1.ModelToSketchSpace(tg.CreatePoint(2.5, 1.5, 1.5));
SketchCircle Circle = sketch1.SketchCircles.AddByCenterRadius(center, 1);
profile = sketch1.Profiles.AddForSolid(true, null, null);
extrudeDefinition = partDefinition.Features.ExtrudeFeatures.CreateExtrudeDefinition(profile, PartFeatureOperationEnum.kJoinOperation);
extrudeDefinition.SetDistanceExtent(4, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection);
extrude = (ExtrudeFeature)partDefinition.Features.ExtrudeFeatures.Add(extrudeDefinition);
Edges cylinderEdges = extrude.SideFaces[1].Edges;
EdgeCollection filletEdges = inventor.TransientObjects.CreateEdgeCollection(null);
//foreach (var el in cylinderEdges)
// filletEdges.Add(el);
filletEdges.Add(cylinderEdges[2]);
//adding fillet
partDefinition.Features.FilletFeatures.AddSimple(filletEdges, 0.25, false, false, false, false, false, true);
//doc.SaveAs("D:\\SaveTest2.ipt", false);
}
private void CalculateHoleB_Click(object sender, EventArgs e)
{
if (inventor == null)
{
MessageBox.Show("No inventor instance detected", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
AssemblyDocument doc = inventor.ActiveDocument as AssemblyDocument;
if(doc == null || doc.ComponentDefinition == null)
return;
AssemblyComponentDefinition partComponentDefinition = doc.ComponentDefinition;
var wall = partComponentDefinition.Occurrences.get_ItemByName("wall:1");
var weight = partComponentDefinition.Occurrences.get_ItemByName("weight:1");
PartComponentDefinition weightDefinition = weight.Definition as PartComponentDefinition;
PartComponentDefinition wallDefinition = wall.Definition as PartComponentDefinition;
SurfaceBody surfaceBody2 = inventor.TransientBRep.Copy(weight.SurfaceBodies[1]);
Vector rotationVector = inventor.TransientGeometry.CreateVector(0, 1, 0);
Inventor.Point point = inventor.TransientGeometry.CreatePoint(0, 0, 0);
var collection = inventor.TransientObjects.CreateObjectCollection();
for (int i = 1; i <= 120; i++)
{
Matrix wallMatrix = wall.Transformation, weigthMatrix = weight.Transformation;
weigthMatrix.Invert();
wallMatrix.PreMultiplyBy(weigthMatrix);
SurfaceBody surfaceBody = inventor.TransientBRep.Copy(weight.SurfaceBodies[1]);
wallMatrix.SetToRotation(i * Math.PI / 180, rotationVector, point);
inventor.TransientBRep.Transform(surfaceBody, wallMatrix);
inventor.TransientBRep.DoBoolean(surfaceBody2, surfaceBody, BooleanTypeEnum.kBooleanTypeUnion);
}
NonParametricBaseFeatureDefinition featureDefinition = weightDefinition.Features.NonParametricBaseFeatures.CreateDefinition();
featureDefinition.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType;
collection.Add(surfaceBody2);
featureDefinition.BRepEntities = collection;
NonParametricBaseFeature baseFeature = weightDefinition.Features.NonParametricBaseFeatures.AddByDefinition(featureDefinition);
//CombineFeature combineFeature = doc.Features.CombineFeatures.Add(wall.SurfaceBodies[1], collection, PartFeatureOperationEnum.kCutOperation);
}
}
}