Aller au contenu

Messages recommandés

Posté(e)

Bonjour,

 

J'essai de changer la valeur text d'un attribut qui contient un Field.

 

Par contre, quand ma commande terminer j'obtien un field "####"

 

 

 

public class Class
   {
       private const string BLOCKNAME = "SOL PL";
       private const string TAG_COUNT = "LONG";
       private const string TAG_SPACING = "ESPACEMENT";

       private const string PREFIX = "%<\\AcObjProp Object(%<\\_ObjId ";
       private const string SUFFIX = ">%).Parameter(39).lookupString>%";
       private string id;

       //private string textString;
       private int joistCount;
       private int spacing;

       enum DrawingUnit { Imperial, Metrique }

       [CommandMethod("CS", CommandFlags.UsePickSet)]
       public void CalculSolives()
       {
           Document document = Active.Document;
           Database database = Active.Database;
           Editor editor = Active.Editor;

           PromptSelectionResult psResults = editor.SelectImplied();
           if (psResults.Status == PromptStatus.Error)
           {
               // Adds options to the Results.
               PromptSelectionOptions psOptions = new PromptSelectionOptions();

               psOptions.MessageForAdding = "\nSélectionner les solives à calculer : ";

               // this captures the prompt values of the selected object
               psResults = editor.GetSelection(psOptions);
           }

           if (psResults.Status == PromptStatus.OK)
           {
               PromptDoubleResult pdResult = editor.GetDistance("Pour la distance utilisé par la/les solive(s). Choisir deux point. :");

               double distance = pdResult.Value;

               using (var transaction = database.TransactionManager.StartTransaction())
               {
                   ObjectId[] objIDs = psResults.Value.GetObjectIds();
                   foreach (ObjectId objID in objIDs)
                   {
                       RXClass attClass = RXClass.GetClass(typeof(BlockReference));
                       if (objID.ObjectClass.IsDerivedFrom(attClass))
                       {
                           BlockReference blockReference = transaction.GetObject(objID, OpenMode.ForRead) as BlockReference;

                           AttributeCollection attCollection = blockReference.AttributeCollection;

                           foreach (ObjectId attId in attCollection)
                           {
                               AttributeReference attReference = transaction.GetObject(attId, OpenMode.ForRead) as AttributeReference;

                               if (attReference.Tag == TAG_SPACING)
                               {
                                   spacing = getSpacing(attReference);
                               }

                               if (attReference.Tag == TAG_COUNT || attReference.HasFields == true)
                               {
                                   ObjectId fieldId = attReference.GetField();

                                   attReference.UpgradeOpen();

                                   id = fieldId.ToString();

                                   id = id.TrimStart('(');
                                   id = id.TrimEnd(')');


                                   joistCount = CalculerSolives(spacing, distance);

                                   string newString = {:content:}quot;({joistCount}) P.ALU {PREFIX}{id}{SUFFIX}";
                                   attReference.TextString = newString;
                               }
                           }
                       }
                   }
                   transaction.Commit();
                   editor.Regen();
               }
           }
       }

       private int getSpacing(AttributeReference attReference)
       {
           int spacing = 0;
           string textString = attReference.TextString;

           int index = textString.IndexOf('"');

           textString = textString.Substring(2, index - 2);

           textString = @textString.Trim('"', 'c', '/');

           try
           {
               spacing = Convert.ToInt32(textString);
           }
           catch (Exception)
           {
               Active.WriteMessage("Couldn't convert to int...");
           }

           if (CheckDrawingUnit() == DrawingUnit.Metrique)
           {
               switch (spacing)
               {
                   case 8:
                       spacing = 200;
                       break;
                   case 10:
                       spacing = 250;
                       break;
                   case 12:
                       spacing = 300;
                       break;
                   case 16:
                       spacing = 400;
                       break;
                   case 19:
                       spacing = 490;
                       break;
               }
           }
           return spacing;
       }

       private DrawingUnit CheckDrawingUnit()
       {
           if (Application.GetSystemVariable("DIMLUNIT").Equals(4))
           {
               return DrawingUnit.Imperial;
           }
           else
           {
               return DrawingUnit.Metrique;
           }
       }

       public int CalculerSolives(int espacement, double distance)
       {
           double nombreSolives = distance / espacement;

           Math.Round(nombreSolives, 0, MidpointRounding.ToEven);
           return Convert.ToInt32(nombreSolives);
       }

       public Field getField(Transaction tr, AttributeReference attReference)
       {
           if (attReference.HasFields)
           {
               Field field = Active.Database.TransactionManager.GetObject(attReference.GetField(), OpenMode.ForRead) as Field;

               return field;
           }
           return null;
       }
   }

   public static class Active
   {
       /// <summary>
       /// Returns the active Editor object.
       /// </summary>
       public static Document Document => Application.DocumentManager.MdiActiveDocument;


       /// <summary>
       /// Returns the active Database object.
       /// </summary>
       public static Database Database => Document.Database;

       /// <summary>
       /// Returns the active Editor object.
       /// </summary>
       public static Editor Editor => Document.Editor;


       /// <summary>
       /// Sends a string to the command line in the active Editor
       /// </summary>
       /// <param name="message">The message to send.</param>
       public static void WriteMessage(string message) => Editor.WriteMessage(message);

       /// <summary>
       /// Sends a string to the command line in the active Editor using String.Format.
       /// </summary>
       /// <param name="message">The message containing format specifications.</param>
       /// <param name="parameter">The variables to substitute into the format string.</param>
       public static void WriteMessage(string message, params object[] parameter) => Editor.WriteMessage(message, parameter);    }

Créer un compte ou se connecter pour commenter

Vous devez être membre afin de pouvoir déposer un commentaire

Créer un compte

Créez un compte sur notre communauté. C’est facile !

Créer un nouveau compte

Se connecter

Vous avez déjà un compte ? Connectez-vous ici.

Connectez-vous maintenant
×
×
  • Créer...

Information importante

Nous avons placé des cookies sur votre appareil pour aider à améliorer ce site. Vous pouvez choisir d’ajuster vos paramètres de cookie, sinon nous supposerons que vous êtes d’accord pour continuer. Politique de confidentialité