Javascript function to modify entity won't modify color bylayer

Javascript function to modify entity won't modify color bylayer

Anonymous
Not applicable
1,236 Views
2 Replies
Message 1 of 3

Javascript function to modify entity won't modify color bylayer

Anonymous
Not applicable

I've been experimenting with the JavaScript API and I couldn't find a way to modify entities so I tried creating my own function that uses AutoLisp to modify DXF codes. I tried using it to modify the color of an entity and it works except in the case where the entity is set to bylayer. Please see function setDXF in the following code.

 

file: jsEntMod.js

function jsEntMod() {

  document.getElementById("modifyBtn").onclick = modifyEnt;
  document.getElementById("selectBtn").onclick = selectEnt;
  document.getElementById("inspectBtn").onclick = inspectEnt;
  
  modifyElements = document.getElementById("modifyElements");
  modifyElements.style.display = "none";

  var selectedEnt;

  function selectEnt(){
    var pEOptions = new Acad.PromptEntityOptions("Chose Entity: ");
    pEOptions.allowNone = false;
    Acad.Editor.getEntity(pEOptions).then(gotEntity,gotEntityFail);
  }

  //callback for selectEnt function
  function gotEntity(args){
    selectedEnt = new Acad.DBEntity(args.objectId);
    if (selectedEnt.entityId != 0){
      modifyElements.style.display = "block";
    }else{
      modifyElements.style.display = "none";
    }
  }

  function gotEntityFail(args){
    alert("Error: No entity selected.");
  }

  function inspectEnt(){
    alert(JSON.stringify(selectedEnt.getProperties()));
  }

  function modifyEnt(){
    var dxfCode = document.getElementById("DXFCode").value;
    var dxfValue = document.getElementById("DXFValue").value;
    setDXF(selectedEnt.entityId, dxfCode, dxfValue);
  } 
  
  function setDXF( objId, dxfCode, dxfValue){

    var obj = new Acad.DBEntity(objId);
    var objHand = obj.getProperties().Handle;

    var lispStr = ""; 

    //Build string of AutoLisp commands to be executed

    lispStr += "(progn ";

    lispStr += "  (setq ent (entget (handent \"" + objHand + "\")))";                                            //get entity's definition data from object
    lispStr += "  (setq ent (subst (cons " + dxfCode + " " + dxfValue + ") (assoc " + dxfCode + " ent) ent ))";  //update the code with the new value
    lispStr += "  (entmod ent)";                                                                                 //update entity

    lispStr += ")"; 
     
    Acad.Editor.executeCommand(lispStr);

  }


}

var oldLoader = window.onload || function() {};
window.onload = function() {
  jsEntMod();
  oldLoader.call(this);
}

 

Can anyone see where I went wrong here?

 

Other files in my project:

 

file: jsEntMod.htm

<html>
  <head>
    <script type="text/javascript" src="jsEntMod.js"></script>
    <script type="text/javascript" src="https://df-prod.autocad360.com/jsapi/v3/Autodesk.AutoCAD.js"></script>
  </head>
  <body>
    <input id="selectBtn" type="button" value="Select" />
    
    <div id="modifyElements">
      <input id="inspectBtn" type="button" value="Inspect" />
      <br />
      DXFCode:
      <br />
      <input id="DXFCode" type="text" />
      <br /> = 
      <br /><input id="DXFValue" type="text" />
      <br />
      <input id="modifyBtn" type="button" value="Modify" />
      <br />
    </div>
  </body>
</html>

 

file:jsEntModPal.js

Acad.Editor.addCommand("JSCOMMAND","JSENTMOD","JSENTMOD",Acad.CommandFlag.TRANSPARENT,jsEntModloadPalette);

function jsEntModloadPalette() {
  Acad.Application.addPalette("Modify Entity", "H:\\AcadJS\\jsEntMod\\jsEntMod.htm");
}

file: jsEntMod.lsp

 

(command "_.WEBLOAD" "L" "H:\\AcadJS\\jsEntMod\\jsEntModPal.js")

 

 

Thank you for any help you can provide,

Brian

0 Likes
Accepted solutions (1)
1,237 Views
2 Replies
Replies (2)
Message 2 of 3

SeeMSixty7
Advisor
Advisor
Accepted solution

When an Entity has a color setting of bylayer it means the DXF code is not defined. Your Subst function finds nothing to substitute as the old value does not exist. You will need to append the 62 dxf code value to the data list.

 

Hopefully that helps,

 

 

Message 3 of 3

Anonymous
Not applicable

Thank you SeeMSixty7. I've modified the function and it works now even if the dxfCode 62 is not yet set.

 

function setDXF( objId, dxfCode, dxfValue){

    var obj = new Acad.DBEntity(objId);
    var objHand = obj.getProperties().Handle;

    var lispStr = ""; 

    //Build string of AutoLisp commands to be executed

    lispStr += "(progn ";
    lispStr += "(setq ent (entget (handent \"" + objHand + "\")))";                                              //get entity's definition data from object
    lispStr += "(if (assoc " + dxfCode + " ent)";                                                                //if the DXF code already exists
    lispStr += "  (setq ent (subst (cons " + dxfCode + " " + dxfValue + ") (assoc " + dxfCode + " ent) ent ))";  //  update the code with the new value
    lispStr += "  (entmod (append ent (list (cons " + dxfCode + " " + dxfValue + "))))";                            //  else add the DXF code to the data definition
    lispStr += ")";

    lispStr += "(entmod ent)";                                                                                   //update entity
    lispStr += ")";  
     
    Acad.Editor.executeCommand(lispStr);

  }