Drawing file - how to identify a TextBox with unique Id or by name

Drawing file - how to identify a TextBox with unique Id or by name

Anonymous
Not applicable
1,731 Views
7 Replies
Message 1 of 8

Drawing file - how to identify a TextBox with unique Id or by name

Anonymous
Not applicable

Hi all,

 

The TextBoxes don't have a Name property or an UniqueIdentifier property.  So, I don't know how to find a specified TextBox.

 

I've tried to add a XML comment by code in the FormattedText property and got an error.

 

 

Anyone know a solution?

 


Thanks,

 

Pascal

0 Likes
Accepted solutions (1)
1,732 Views
7 Replies
Replies (7)
Message 2 of 8

ChrisAtherton
Advocate
Advocate

I've added attributes to mine.  These are a bit like hidden iProperties for objects in Inventor:

http://adndevblog.typepad.com/manufacturing/2013/01/inventor-set-attributes-through-api.html

 

By adding them you can search for attributes of a certain name or value then use them to select the associated object.

Chris Atherton
IEng MIMechE BEng Hons
Design Automation Services Manager | Symetri
https://uk.linkedin.com/in/chrissoathe
Message 3 of 8

jdkriek
Advisor
Advisor

Attributes are lovely aren't they? 😉

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 4 of 8

Anonymous
Not applicable

Hi Chris,

 

I am able to add an AttributeSet (AttributeSets.Count = 1) but, when I edit the sketch the count is 0.

 

What I doing wrong?

 

 

Pascal

0 Likes
Message 5 of 8

xiaodong_liang
Autodesk Support
Autodesk Support

Hi Pascal,

 

Which object you added the attribute? Are you sure it was Sketch? Please provide a small demo on what you are doing. It will help to know what happened.

0 Likes
Message 6 of 8

Anonymous
Not applicable

Hi Xiaodong,

Here's a little C# demo that reproduces the problem.  I tried to send you the complete solution (in a .zip file) but I get an error: "The file is corrupted.  Please update a valid file".  So, I attach a .txt file that represent the Program.cs file.

Before starting, you must create a new drawing with a TitleBlockDefinition that must include one text box.

The demo use the first TitleBlockDefinition in the active drawing document and showing the count of AttributeSet of the first TextBox.

 

Here's the result:

Result.jpg

 

Thanks, 

Pascal Langlais

0 Likes
Message 7 of 8

adam.nagy
Autodesk Support
Autodesk Support
Accepted solution

Hi Pascal,

 

2 things:

1) Sketch editing of title block definition, i.e. Edit() / ExitEdit(), seems to rely on a copy of the sketch, so you would need to add the attribute set with CopyWithOwner = True

2) All sketch editing of the title block definition should be done inside an Edit() / ExitEdit() section including modifying the attributes of a text box inside the sketch 

 

Once I followed the above the attributes remained in tact:

static void test()
{
  Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;

  if (Application.ActiveDocument == null)
    Console.WriteLine("There is no active document in Inventor.");
  else
    if (!(Application.ActiveDocument is DrawingDocument))
      Console.WriteLine("Active document should be a drawing document.");
    else
    {
      // Get active drawing document
      Document = Application.ActiveDocument as DrawingDocument;

      // Get ANSI large title block definition 
      TitleBlockDefinitions titleBlockDefinitions = Document.TitleBlockDefinitions;
      TitleBlockDefinition titleBlockDef1 = titleBlockDefinitions[1];

      // Show counts of attribute set and attribute (should be 0)
      TextBox textBox = titleBlockDef1.Sketch.TextBoxes[1];
      Console.WriteLine("AttributeSets.Count => " + textBox.AttributeSets.Count.ToString() + " , Expected = 0 (Before creating)");

      ///////////////////////////////////////////////////////////////////
      // Real modification, adding the Attribute set
      ///////////////////////////////////////////////////////////////////

      // Edit sketch
      DrawingSketch sketch;
      titleBlockDef1.Edit(out sketch);

      // Add Attribute set and attribute
      textBox = sketch.TextBoxes[1]; 
      AttributeSet myAttributeSet = textBox.AttributeSets.Add("MySet", true);
      Inventor.Attribute myAttribute = myAttributeSet.Add("MyAttribute", ValueTypeEnum.kStringType, "MyAttributeValue");

      // Exit sketch with saving
      titleBlockDef1.ExitEdit(true);

      // Show counts of attribute set and attribute (should be 1)
      textBox = titleBlockDef1.Sketch.TextBoxes[1];
      Console.WriteLine("AttributeSets.Count => " + textBox.AttributeSets.Count.ToString() + " , Expected = 1 (After creating, before editing sketch)");

      ///////////////////////////////////////////////////////////////////
      // Fake modification, just for testing that the Attribute remains
      ///////////////////////////////////////////////////////////////////

      //DrawingSketch sketch;
      titleBlockDef1.Edit(out sketch);

      // Exit sketch with saving
      titleBlockDef1.ExitEdit(true);

      // Show counts (ERROR USED TO BE HERE => should be 1 but previously showed 0)
      textBox = titleBlockDef1.Sketch.TextBoxes[1];
      Console.WriteLine("AttributeSets.Count => " + textBox.AttributeSets.Count.ToString() + " , Expected = 1 (After ExitEdit)");
      Console.ReadKey();
    }
}

Cheers,



Adam Nagy
Autodesk Platform Services
Message 8 of 8

Anonymous
Not applicable

Hi Adam,

 

Thanks for your explanations, it seems logical.

 

Leaving on holidays now but I will have a look at your solution upon my return.

 

Kind regards,

 

Pascal

0 Likes