This section of VBA procedures converted from the help file are related to various topics. A couple of the VBA examples in this section use an InputBox to get values from the user. The C# code uses a form instead. The form is used with the attribute and units of measure example. Also The C# code does have the thumbnail example but it is not on the dropdown because using iPictureDisp needs to run in process. If you need to do change the thumbnail of a document you can copy the code to an Inventor AddIn. The PictureDispConverter class is a C# version of the VB code example on this post.
Even though the name of this section is Miscellaneous the classes that these examples demonstrate will be used in many Inventor applications. (This is the eleventh post with VBA examples converted to C#).
You can find details about how the C# projects can be used in this post. This project has the following functions:
Download InventorHelpExamples_General_Miscellaneous
SetAndGetAttribute
ExplodeClientFeature
ExportToSat
PlaceContentCenterPart
addAppEvents
removeAppEvents
OpenDocumentWithRepresentations
PrintEnvironmentNames
ReferenceKeyFromFace
FaceFromReferenceKey
InventorUOM
InventorUOMConvert
CreateUCSBy3Points
CreateUCSByTransformationMatrix
Here is the SetAndGetAttribute function:
// Dynamic Attributes API Sample
//Description
//This sample demonstrates the basic concept of
//dynamic attributes. For a selected entity, it
//creates an attribute set and an attribute. If
//the selected entity already has the attribute
//set, it allows you to edit the value.
//To use this sample select an entity and run.
public void SetAndGetAttribute()
{
// Check to make sure a single item is in
//the select set.
if(ThisApplication.ActiveDocument.
SelectSet.Count != 1)
{
MessageBox.Show
("A single entity must be selected.");
return;
}
// Get the item from the select set.
object oSelectedObject = null;
oSelectedObject =
ThisApplication.ActiveDocument.SelectSet[1];
// Make sure the selected object
//supports attributes.
AttributeSets oAttribSets =
default(AttributeSets);
try
{
oAttribSets = (AttributeSets)
oSelectedObject.GetType().InvokeMember
("AttributeSets",
BindingFlags.GetProperty,
null, oSelectedObject, null);
}
catch
{
MessageBox.Show
("Object does not support attributes.");
return;
}
string sNewValue = null;
AttributeSet oAttribSet =
default(AttributeSet);
Inventor.Attribute oAttrib =
default(Inventor.Attribute);
// Check to see if the object already
//has attribute set named "AttribTest".
if (oAttribSets.NameIsUsed["AttribTest"])
{
// Get a reference to the existing
//attribute set.
oAttribSet =
oAttribSets["AttribTest"];
// Get the existing attribute.
oAttrib = oAttribSet["Attrib"];
using(myInputForm myForm =
new myInputForm())
{
myForm.textBox1.Text =
oAttrib.Value;
if(myForm.ShowDialog(this)
== DialogResult.OK)
{
sNewValue =
myForm.textBox1.Text;
}
}
// If the value's different, change
//the value of the attribute.
if (!string.IsNullOrEmpty(sNewValue)
& sNewValue != oAttrib.Value)
{
oAttrib.Value = sNewValue;
}
}
else
{
//Get value to assign to attribute.
using (myInputForm myForm =
new myInputForm())
{
if (myForm.ShowDialog(this)
== DialogResult.OK)
{
sNewValue =
myForm.textBox1.Text;
}
}
// If a value was entered, create
//new attribute set and attribute.
if (!string.IsNullOrEmpty(sNewValue))
{
// Create a new attribute set
//with the name "AttribTest".
oAttribSet =
oAttribSets.Add("AttribTest");
// Create new attribute named
//"Attrib" and assign value
oAttrib = oAttribSet.Add
("Attrib",
ValueTypeEnum.kStringType,
sNewValue);
}
}
}
-Wayne


Leave a Reply