Message 1 of 3
Not applicable
05-15-2017
07:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.