LDCad Simple LDCad script


Simple LDCad script
#1
So, I wrote my first script for LDCad, hooray! Cool

It is a simple macro to transpose two colors in a selection. You might use it if you have built a huge checkered floor out of tiles and realized you have the pattern backwards!

I'm sure there are ways to improve it, but I'm actually pleased I was able to work it out in one day. Big Grin

Here it is:
Code:
genTools=require('genTools') function runColorSwap()   local ses=ldc.session()   local sel=ses:getSelection()   local cnt=sel:getRefCount()     if not ses:isLinked() then     ldc.dialog.runMessage('No active model.')     return   end   if cnt==0 then     ldc.dialog.runMessage('No selection active.')     return   elseif cnt<2 then     ldc.dialog.runMessage('At least two items must be selected.')     return   end   local colors={}     local function has_value (tab, val)     for k, v in ipairs(tab) do         if v == val then             return true         end     end     return false   end   for i=1,cnt do     local ref=sel:getRef(i)     local col=ref:getColor()     if not has_value(colors, col) then       table.insert(colors, col)     end   end   if #colors==2 then     for i=1,cnt do       local ref=sel:getRef(i)       local col=ref:getColor()       if col==colors[1] then         ref:setColor(colors[2])       elseif col==colors[2] then         ref:setColor(colors[1])       end     end   else ldc.dialog.runMessage('The selection must contain exactly two colors.')   end end --=================== function register()   local macro=ldc.macro('Color swap')   macro:setHint('Transposes two colors in a selection.')   macro:setEvent('run', 'runColorSwap') end register()

Suggestions are welcome!
Reply
« Next Oldest | Next Newest »



Messages In This Thread
Simple LDCad script - by N. W. Perry - 2022-03-25, 4:27
RE: Simple LDCad script - by Roland Melkert - 2022-03-25, 19:56
RE: Simple LDCad script - by N. W. Perry - 2022-03-25, 22:33
RE: Simple LDCad script - by Roland Melkert - 2022-03-25, 22:57
RE: Simple LDCad script - by N. W. Perry - 2022-03-26, 2:00
RE: Simple LDCad script - by Yx Wang - 2025-02-13, 10:06
RE: Simple LDCad script - by Roland Melkert - 2025-02-13, 18:13
RE: Simple LDCad script - by HWQ - 2025-06-26, 6:10
RE: Simple LDCad script - by Roland Melkert - 2025-06-28, 19:55
RE: Simple LDCad script - by HWQ - 2025-07-02, 2:41
RE: Simple LDCad script - by Roland Melkert - 2025-07-02, 20:23

Forum Jump:


Users browsing this thread: 1 Guest(s)