.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to change the value of User Variables of autocad201​0 through .net API

2 REPLIES 2
Reply
Message 1 of 3
zhangxinhua789
578 Views, 2 Replies

how to change the value of User Variables of autocad201​0 through .net API

aa.jpg

 

Hello,experts

this is a screenshot of the dwg-file。

  I want to change the value of user1  through  .net API(user1 is a User Variables),my purpose is to change the length of the line.could you tell me the code ? Thank you very much!

 

 

 

 

 

2 REPLIES 2
Message 2 of 3

hello, this is the dwg-file

Message 3 of 3

using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcEd = Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Rivilis.UserVars))]

namespace Rivilis
{
  public class UserVars
  {
    [CommandMethod("ChangeUserVar")]
    public static void ChangeUserVar()
    {
      AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
      AcEd.Editor ed = doc.Editor;
      AcDb.ObjectId idNet = AcDb.AssocNetwork.GetInstanceFromDatabase(doc.Database, true, null);
      using (AcDb.AssocNetwork pNetMain = idNet.Open(OpenMode.ForRead) as AcDb.AssocNetwork) {
        AcDb.ObjectIdCollection ids = pNetMain.GetActions;
        if (ids.Count == 0) {
          ed.WriteMessage("\nNo user variable is found!");
          return;
        }
        List<string> varsName = new List<string>();
        using (AcDb.AssocNetwork pNetSub = ids[0].Open(OpenMode.ForWrite) as AcDb.AssocNetwork) {
          if (GetVars(pNetSub, ref varsName, ref ids) == 0) {
            ed.WriteMessage("\nNo user variable is found!");
            return;
          }
        }
        AcEd.PromptResult rs1 = ed.GetString("\nVariable name: ");
        if (rs1.Status != PromptStatus.OK) return;
        string varName = rs1.StringResult;
        if (!varsName.Contains(varName)) {
          ed.WriteMessage("\nVariable {0} not found", varName);
          return;
        }
        AcEd.PromptResult rs2 = ed.GetString("\nVariable expression: ");
        if (rs2.Status != PromptStatus.OK) return;
        string varValue = rs2.StringResult;
        int i = varsName.FindIndex(delegate(string s) { return s == varName; });
        if (i >= 0 && i < varsName.Count) {
          using (AcDb.AssocVariable pVar = ids[i].Open(OpenMode.ForWrite) as AcDb.AssocVariable) {
            string errmsg = string.Empty;
            try {
              pVar.ValidateNameAndExpression(varName, varValue, ref errmsg);
              if (errmsg == string.Empty) {
                pVar.SetExpression(varValue, "", true, true, ref errmsg, true);
                ResultBuffer evalVal = null;
                pVar.EvaluateExpression(ref evalVal);
                pVar.Value = evalVal;
              } else {
                ed.WriteMessage("\n Error in ValidateNameAndExpression = {0}", errmsg);
              }
            } catch (System.Exception ex) {
              ed.WriteMessage(ex.ToString());
            }
          }
        }
      }
    }
    //////////////////////////////////////////////////////////////////////////
    // Function get lists of variable names and its ObjectIds
    //////////////////////////////////////////////////////////////////////////
    private static int GetVars(AcDb.AssocNetwork pNet, ref List<string> names, ref AcDb.ObjectIdCollection ids)
    {
      names.Clear(); ids.Clear();
      AcDb.ObjectIdCollection idsActions = pNet.GetActions;
      for (int i = 0; i < idsActions.Count; i++) {
        using (AssocAction pAction = idsActions[i].Open(OpenMode.ForRead) as AssocAction) {
          AcDb.AssocVariable pVar = pAction as AcDb.AssocVariable;
          if (pVar != null) {
            names.Add(pVar.Name); ids.Add(pVar.ObjectId);
          }
        }
      }
      return ids.Count;
    }
  }
}

Also very useful link:

Working with Associative Parameters from API

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost