By Xiaodong Liang
(Continued)
In the past post, we got a skeleton of a translator add-in. By this translator, we can open a data source *.abc, and save the geometries of Inventor to a *.abc file.
To make a simpler demo, we define the *.abc as a text file. It has 3 lines:
1. Three double, split by the comma (for center point of a sphere)
2. A double (for radius of a sphere)
3. A string (for iProperties >> part number)
In Open method of the add-in. we will open a *.abc file, create a sphere with data of line 1 & 2, and update iProperties >> part number with line 3. In the SaveCopyAs method, we will get one existing sphere face of a part, save center point coordinates and radius to the *.abc file. And save iProperties >> part number to the file.
publicvoid Open(DataMedium SourceData,
TranslationContext Context,
NameValueMap Options,
refobject TargetObject)
{
// When Open *.abc, this function will
// be invoked
//the file name of the data source
string fileName = SourceData.FileName;
// the params read from the data source
//center point of sphere
Inventor.Point oCenterPt =
m_inventorApplication.TransientGeometry.CreatePoint();
//radius of sphere
double radius = 0;
//part number
string partNumber = "";
try
{
//read the data source
using (StreamReader sr = newStreamReader(fileName))
{
String line;
int rowIndex = 0;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
if (rowIndex == 0)
{
//center point of sphere
string[] oStrArray = line.Split(newchar[]{','});
oCenterPt.X = Convert.ToDouble(oStrArray[0]);
oCenterPt.Y = Convert.ToDouble(oStrArray[1]);
oCenterPt.Z = Convert.ToDouble(oStrArray[2]);
}
elseif (rowIndex == 1)
//radius of sphere
radius = Convert.ToDouble(line);
elseif(rowIndex == 2)
//part number
partNumber = line;
else
//in this demo, we are interested in
// the 3 params only
break;
rowIndex++;
}
}
try
{
if (radius > 0)
{
//if the radius is valid
//create a new part document
PartDocument oNewDoc =
m_inventorApplication.Documents.Add(
DocumentTypeEnum.kPartDocumentObject)
asPartDocument;
//get ComponentDefinition of the part
PartComponentDefinition oDocDef =
oNewDoc.ComponentDefinition;
//get TransientBRep
TransientBRep oTB =
m_inventorApplication.TransientBRep;
//create a transient body of a sphere
SurfaceBody oSB =
oTB.CreateSolidSphere(oCenterPt, radius);
//add the transient body to the part as a
// non ParametricBaseFeature
oDocDef.Features.
NonParametricBaseFeatures.Add(oSB);
//update the iProperties with the part
//number from the data source
Inventor.PropertySet oPS =
oNewDoc.PropertySets["Design Tracking Properties"];
oPS.ItemByPropId[
(int)PropertiesForDesignTrackingPropertiesEnum.
kPartNumberDesignTrackingProperties].Value = partNumber;
m_inventorApplication.ActiveView.Fit();
}
else
{
MessageBox.Show("the radius is invalid!");
}
}
catch (Exception Exception1)
{
MessageBox.Show("fail to create model!\n" +
Exception1.ToString());
}
}
catch(Exception Exception){
MessageBox.Show("reading data failed!\n" +
Exception.ToString());
}
}
publicvoid SaveCopyAs(object SourceObject,
TranslationContext Context,
NameValueMap Options,
DataMedium TargetData)
{
// When save to *.abc, this function will
// be invoked
//the file to be saved
string fileName = TargetData.FileName;
PartDocument oPartDoc =
m_inventorApplication.ActiveDocument
asPartDocument;
PartComponentDefinition oPartDef =
oPartDoc.ComponentDefinition;
if (oPartDef.Features.RevolveFeatures.Count > 0)
{
//if there is revolve feature in the part
//get one revolve feature
//which is full sphere surface
bool found = false;
Inventor.Point centerPt =
m_inventorApplication.TransientGeometry.CreatePoint();
double radius = 0;
string partNumber = "";
foreach (RevolveFeature oRF
in oPartDef.Features.RevolveFeatures)
{
if (oRF.Faces.Count == 1 )
{
Face oF = oRF.Faces[1];
if(oF.Edges.Count == 0 &&
oF.Vertices.Count ==0 )
{
//this is a full sphere
found = true;
SurfaceEvaluator oSE = oF.Evaluator;
//calculate radius
radius = Math.Sqrt(oSE.Area / 4 * Math.PI);
//calcuate center point
Inventor.Point maxPt = oSE.RangeBox.MaxPoint;
Inventor.Point minPt = oSE.RangeBox.MinPoint;
centerPt.X = (maxPt.X - minPt.X) / 2.0;
centerPt.Y = (maxPt.X - minPt.Y) / 2.0;
centerPt.Z = (maxPt.X - minPt.Z) / 2.0;
}
}
}
if (!found)
{
MessageBox.Show("no revolve feature which is full sphere!");
return;
}
//get the iProperties >> part number
Inventor.PropertySet oPS =
oPartDoc.PropertySets["Design Tracking Properties"];
partNumber = oPS.ItemByPropId[
(int)PropertiesForDesignTrackingPropertiesEnum.
kPartNumberDesignTrackingProperties].Value.ToString();
//write the data to the *.abc
using (StreamWriter writer =
newStreamWriter(fileName))
{
string centerPtStr=centerPt.X + "," +
centerPt.Y + "," +
centerPt.Z;
writer.WriteLine(centerPtStr );
writer.WriteLine(radius.ToString());
writer.WriteLine(partNumber);
}
}
else
{
MessageBox.Show("no revolve feature!");
}
}
The full project can be downloaded at: Download MyTranslator
As you remember, we can customize the options of the built-in translators. It is same to custom translator add-in.In the demo above, we define the behaviors directly without taking any options into account. In the next post, we will see how to control the options.
(to be continued)