Moving and rotating object using the API.
325 23
pommie 2 months ago
Hi All, I've got a board with over 200 LEDs on it and need to position them using code. I have a list of positions and rotations but can't work out how to do this in code. I can fetch the board using var board = api('getSource', {type: "json"}); and looking at the object, FOOTPRINT seems to be the area where my LEDs are stored but with a strange name (first is gge0c458c32494ef67c). Where (or how) can I find the position of LED1 and once found change it's position and angle? Or, is there a different API call to move/rotate objects? Looks like I need to use moveObjsTo but how do I find the name of the objects I need to move? I've tried api('moveObjsTo', {objs:[{gId:"LED1"}], x:20, y:20}); but think the name of the object is not LED1. Any help greatly appreciated. Mike. Edit, I've shared the project publicly (I think) under my user name (pommie). I think the URL is [https://oshwlab.com/pommie/tic-tac-toe](https://oshwlab.com/pommie/tic-tac-toe)<br> <br>
Comments
pommie 2 months ago
Just tried doing api('moveObjsTo', {objs:[{gId:"gge0c458c32494ef67c"}], x:20, y:20}); and it moved LED185 to somewhere of the screen. I need to move known objects to a position relative to 0,0, just like if you type in the X and Y position boxes. This is what I see,![EasyEDA.png](//image.easyeda.com/pullimage/KPwKKuZDHzCN47if1CxVBaAVum6UAIyKvUZOmb5D.png) Thanks for any help. Mike.
Reply
pommie 2 months ago
Also, before executing the above script, my board was at position 0,0 and now it's at 246,-245. What happened? Mike.
Reply
andreasbernhofer 2 months ago
So, first of all, all coordinates the API uses are absolute to the top left corner of the editor. The origin marked in the editor is just a cross that can be at any position. To get the absolute position of the origin, read the canvas attribute of the source. e.g: ``` js let s = api('getSource', {type: "json"}); console.log([s.canvas.originX,s.canvas.originY]); ``` Also, all canvas coordinates used are in 1/100 inch. You can convert your own or use the API: ``` js api('valConvert', {type:'real2canvas',val:'20mm'}); // 20mm to canvas api('valConvert', {type:'real2canvas',val:'20mil'}); // 20mil to canvas api('valConvert', {type:'canvas2real',val:'20mm'}); // 20 canvas pixels to mm api('valConvert', {type:'canvas2real',val:'20mil'}); // 20 canvas pixels to mil ``` To iterate over (and then filter) selected objects, you can use this: ``` js api('getSelectedIds').split(',').forEach((k)=>{ footprint = api('getShape',{id: k}); console.log(footprint); }); ``` You can then move and rotate them. Hope this helps so far, I may come back to this later, have other stuff to do now.
Reply
pommie 2 months ago
That is very useful. I've now got the following script, `console.clear();` `var s = api('getSource', {type: "json"});` `var LEDs = [];` `for (let x in s.FOOTPRINT){` `    for(let i in s.FOOTPRINT[x].TEXT){` `        var id = s.FOOTPRINT[x].TEXT;` `        var name = s.FOOTPRINT[x].TEXT[i].text;` `        if(name.slice(0,3)=="LED"){` `            LEDs.push([name,x]);` `        }` `    }` `}`<br><br>`LEDs.forEach(myFunc);`<br><br>`function myFunc(value,index){` `    console.log(value[0] + "," + value[1]);` `}` Which gives me a list of LEDs and there IDs. Got to do other stuff now but will try using the moveObjsTo API call later to move things around. Mike.
Reply
pommie 2 months ago
Just to add, I don't like the way the forum removes all the indenting from the inline code. Is there a way to keep the formatting? Also, is there an edit facility for previous posts? Mike.
Reply
pommie 2 months ago
OK, I've got all the LEDs positioned correctly. However, rotation seems to be relative. If I say api('rotate', {ids:[id],degree:90}); it rotates the element 90 degrees clockwise - not to 90 degrees. Is there an equivalent to moveObjsTo such as rotateObjsTo? Yes, I have tried that. Thanks, Mike.
Reply
pommie 2 months ago
Found a work around, if I select all the LEDs prior to running the script and set the angle to zero, I can then run the script with the angle negated to get the correct rotations. The finished article does look pretty. ![LEDs.png](//image.easyeda.com/pullimage/Ij1pG0Bu5L4xtJZ8024uaE1KaMhYAd9m5Ca7UeSN.png) Mike.
Reply
pommie 2 months ago
Yes, I spotted it too, the wonky diagonal LED due to a typing error (34 instead of 45). Corrected now. BTW, thanks  [andreasbernhofer](https://easyeda.com/andreasbernhofer), your help is much appreciated. <br> Mike.
Reply
pommie 2 months ago
I've now routed the board and the autorouter reports 100% complete. ![Noughts1.png](//image.easyeda.com/pullimage/RtFmNp2tI63UBFxeu9hNnFCvLsEYFRh34B2PBYBd.png) But when I run a DRC it reports lots of errors. For example, ![Noughts2.png](//image.easyeda.com/pullimage/kZbLUXYm4IvWswIy5LaC4k5prd7BV08UdKIadArl.png) In the bottom left corner it states "Incomplete connection, please check" However, the connection looks perfectly good. Can anyone tell me what is wrong and if it is fixable. <br> Thanks, Mike.
Reply
UserSupport 2 months ago
@pommie You need to refresh the Nets folder
Reply
pommie 2 months ago
@UserSupport Thanks, new to easyEDA and didn't know if having components at strange angles somehow caused problems. It seems to have fixed (refreshed) itself now. How do I refresh the nets folder in the future? Mike.
Reply
UserSupport 2 months ago
![图片.png](//image.easyeda.com/pullimage/o2lR8SbNqvp1v8O4GrG96G9gcw5ZwVI9XAzqzk1X.png)
Reply
andyfierman 2 months ago
@pommie, "Is there a way to keep the formatting?" Try Blockquote? ![image.png](//image.easyeda.com/pullimage/MzSmWh1Yjq6yEWILIPNq0zhb1eR2LCVhnCFzkAAS.png) "Also, is there an edit facility for previous posts?" ![image.png](//image.easyeda.com/pullimage/fAxZ8yY1sQn9TrjPgDwDhovZHFJMzaAxJHGNVD2u.png)
Reply
pommie 2 months ago
@UserSupport As can be seen on the screenshot above, the refresh icon is hidden on my monitor. I can hide that pane but don't seem to be able to make it wider. Mike.
Reply
UserSupport 2 months ago
@pommie You can resize the sidebar ![图片.png](//image.easyeda.com/pullimage/SAUwt2VBBUpxHK1BW5laQZAvW2dzgxkpKE8k1gNM.png)
Reply
pommie 2 months ago
@UserSupport I tried that but never get the resize arrows - it darkens but the arrows never show. Using Chrome on windows 7 as it says in the corner. Mike.
Reply
andreasbernhofer 2 months ago
Hey, great progress 🥳 I actually switch the editor to "Markdown" mode (bottom right) when posting code. You can then surround code with three back-ticks (fenced code block) like this: ``` ```js /* your code here */ ``` ``` For absolute rotation you could probably also use the `updateShape` API call. I can give more information on that if you like.
Reply
eric 2 months ago
Another solution: ```` js // example script for moving PCB objects var canvasConfig = api('editorCall', {cmd:'canvas_config'}); /** * @param {string} id * @param {number} x * @param {number} y * @param {string} unit - One of 'mm' | 'mil' | 'inch' */ function moveTo(id, x, y, unit) { var xPx = api('unitConvert', {unitOld: unit, unitNew: 'pixel', value: x}); var yPx = api('unitConvert', {unitOld: unit, unitNew: 'pixel', value: y}); api('moveObjsTo', {objs:[id], x: ox + xPx, y: oy - yPx}); } // usage moveTo('gge0c458c32494ef67c', 20, 10, 'mm') ``` ````
Reply
andreasbernhofer 2 months ago
Okay, the fenced code block inside the fenced code block worked fine in the preview but not in the posting ^^ You'll have to look up "markdown fenced code block" yourself ;) sorry for the mess
Reply
pommie 2 months ago
@eric That code would be fantastic. However, the problem was going from a prefix (LED108) to an ID. [andreasbernhofer](https://easyeda.com/andreasbernhofer)'s example above got me there. I'd still like to know how to set a rotation or get a rotation so the required rotation can be calculated. Mike.
Reply
pommie 2 months ago
@andreasbernhofer I would like more information on updateShape if that will let me set an absolute rotation for an object. Mike.
Reply
andreasbernhofer 2 months ago
@pommie sorry, short update... couldn't get `updateShape` to work with footprints... But you can use this to get the current rotation and do an absolute rotation: ``` js id = api('getSelectedIds'); shape = api('getShape',{id: id}); currentRotation = parseInt(shape.head.rotation); targetRotation = 170; api('rotate', {ids:[id],degree: -(targetRotation-currentRotation) }); ``` basically, the rotation is read from the `FOOTPRINT.head.rotation`
Reply
pommie 2 months ago
Thanks, All worked perfectly except any objects with rotation zero which returned NaN!!! No idea why and easily fixed (bodged). Mike.
Reply
Login or Register to add a comment
goToTop
你现在访问的是EasyEDA海外版,使用建立访问速度更快的国内版 https://lceda.cn(需要重新注册)
如果需要转移工程请在个人中心 - 工程 - 工程高级设置 - 下载工程,下载后在https://lceda.cn/editor 打开保存即可。
有问题联系QQ 3001956291 不再提醒
svg-battery svg-battery-wifi svg-books svg-more svg-paste svg-pencil svg-plant svg-ruler svg-share svg-user svg-logo-cn svg-double-arrow
We use cookies to offer you a better experience. Detailed information on the use of cookies on this website is provided in our Privacy Policy. By using this site, you consent to the use of our cookies.