Ad

Thursday, September 25, 2014

Set Reference Text

Nothing like having to create a code snippet test to provide it to Autodesk so they can check to make sure their own code works. It’s not like they are going to provide me the source code so I can fix the error in the method. So I figured I’d create a blog post at the same time so Autodesk can steal it to and hopefully create a unit test so they can make sure non-buggy software doesn’t make it out into the world. Unfortunately for you, at least at the time of this writing, this post is entirely useless for setting reference text for a surface. It does work for alignments, profiles, and some other object types. Feel free to use the code for this. This code doesn’t have any error catching and would need improvements if used in a production environment.

The code throws an exception for setting surfaces as a reference saying it’s not the correct type of object.  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.DatabaseServices;
using Autodesk.Civil.DatabaseServices.Styles;

namespace SetReferenceTextTarget
{
    public class SetReferenceTextTargetTest
    {
        [CommandMethod("SetReferenceTextTargetTest")]
        public static void runCommand()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            try
            {
                using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
                {
                    var surfObjId = ed.GetEntity("Select Surface:").ObjectId;
                    var labelToUse = ed.GetEntity("Select label with surface reference text: ").ObjectId.GetObject(OpenMode.ForWrite) as Label;

                    // Get the reference text, yeah! We can't use link because this "modern" software hasn't implemented IEnumerable for the
                    // ObjectIdCollection yet. So we get to go through each one in a loop!
                    ObjectId refTxtlabelComponentObjId = ObjectId.Null;
                    foreach (ObjectId objIdTxt in labelToUse.GetTextComponentIds())
                    {
                        var txtComp = objIdTxt.GetObject(OpenMode.ForWrite) as LabelStyleReferenceTextComponent;
                        if (txtComp != null)
                        {
                            refTxtlabelComponentObjId = objIdTxt;
                        }
                    }

                    labelToUse.SetReferenceTextTarget(refTxtlabelComponentObjId, surfObjId);

                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Autodesk quality control sucks!: Here is proof: " + ex.Message);
            }
        }
    }
}

2 comments:

Luke said...

Have you figured this out yet. I'm banging my brain on the same issue.
Cheers.

Christopher Fugitt said...

I don't think Autodesk has done anything. I didn't care enough to see if I could do a workaround by looking in dlls or arx hooks.

LinkWithin

Blog Widget by LinkWithin

Ad