Make a lamp using script

Top of Documents
Table of Contents

Make a chip of lighting that includes script.
Turn on and turn off the light when you pull the switch.

  1. Show [Environment setup] dialog.
  2. Show [Interface] tab, then mark [Expert mode] and [Run script in debug mode] checkbox.
  3. Right-click on [Chip table] and execute [Modify chip table] command.
  4. Click [Add] button in [Modify chip table] dialog.
  5. [Chip Editor] will start. Show [Environment setup] dialog and mark [Expert mode] checkbox.
  6. Make a lamp picture.
  7. [A]Arc-object, [B]Polyline-object, [C]Arc-object, [D]Polyline-object
  8. Show [Properties] dialog of the [A]arc-object, then shoe [Dictionary] page. Set 0 in [Numeric type dictionary] > [ID] and [Value] then click [Setup] button.
  9. Set the following values in [Numeric type dictionary].
    These values are used to identify each member object of the chip-object.
    Object [ID] [Value]
    [A] 0 0
    [B] 0 1
    [C] 0 2
    [D] 0 3
  10. Choose [Composite chip mode], then register the chip and exit [Chip Editor].
  11. Place the created chip on the canvas.
  12. Show [Properties] dialog of the chip, then show [Script] tab. Click [Select] button.
  13. [Modify script table] dialog will be displayed. Click [Add] button.
  14. [Create/edit script] dialog will be displayed. Set 'Lighting' in [Script name].
  15. Confirm that 'Lighting' is selected in [Modify script table] dialog.
  16. Confirm that 'Lighting' is set in [Script] tab of [Properties] dialog.
  17. Click the menu [View] > [Show script debugger].
  18. [Script Debugger] will be displayed.
  19. Click the menu [Edit] > [Edit Script].
  20. It will become enabled to edit the script code.
  21. Input the following code.
    
    ResetHandles = function(session, tool, objId)
        local chip = Doc():ObjectIdToObject(objId);
        local membIdArray = chip:GetMemberIdArray();
        for i,memberId in ipairs(membIdArray) do
            local membObj = Doc():ObjectIdToObject(memberId);
            local dictValue = membObj:GetDoubleDictionary(0);
            session:DuplicateAndKeepObject(dictValue, memberId);  -- [*1]
        end
        local pullSwPoint = session:GetKeepedObject(0):GetCenter();
        tool:AddHandle(0, false, pullSwPoint);  -- [*2]
    end
    					
    [ResetHandles] is a function that is called when the control-handles should be created or updated. e.g. When the edit-tool selects this object.
    Code[*1] duplicates each member object of the chip-object. Code[*2] registers the center point of the arc-object[A] as a handle.
    See Lua Reference Manual, etc, User guide:Script and API Reference for Lua for details.
  22. Save the script.
  23. Double-click the chip to activate the script-tool. A handle will appear at the point of the arc-object[A].
  24. You can drag the handle to any place in the canvas.
    Deactivate the script-tool and activate the select-tool.
  25. Activate [Script Debugger] and add the code [*1], [*2] and [*3], then save it.
    
    ResetHandles = function(session, tool, objId)
        local chip = Doc():ObjectIdToObject(objId);
        local membIdArray = chip:GetMemberIdArray();
        for i,memberId in ipairs(membIdArray) do
            local membObj = Doc():ObjectIdToObject(memberId);
            local dictValue = membObj:GetDoubleDictionary(0);
            session:DuplicateAndKeepObject(dictValue, memberId);
        end
        local pullSwPoint = session:GetKeepedObject(0):GetCenter();
        tool:AddHandle(0, false, pullSwPoint);
        tool:AddPolylineHandleRestriction(0, {pullSwPoint,{x=pullSwPoint.x, y=pullSwPoint.y + 10}});  -- [*1]
    end
    
    -- [*2]
    OnStartHandleMoving = function(session, tool, handleId, objId)
        tool:HookObject(0, session:GetKeepedObject(0));
    end
    
    -- [*3]
    OnHandleMoving = function(session, tool, handleId, objId)
        if handleId == 0 then
            local point = tool:GetHandlePoint(handleId);
            session:GetKeepedObject(0):SetCenter(point);
        end
    end
    					
    [OnStartHandleMoving] is a function that is called when you start to drag the handle.
    [OnHandleMoving] is a function that is called while you are dragging the handle.
    Code[*1] restricts the moving area of the handle to a line segment.
    Code[*2] adds the copy of the arc-object[A] to the drawing target list.
    Code[*3] moves the copy of the arc-object[A] and draw its shape.
  26. The handle can move between the center of the arc-object[A] and 10mm below it. And the shape of the arc-object[A] will follow the handle.
  27. Activate [Script Debugger] and add code [*1] and [*2]. Then save it.
    
    ResetHandles = function(session, tool, objId)
        local chip = Doc():ObjectIdToObject(objId);
        local membIdArray = chip:GetMemberIdArray();
        for i,memberId in ipairs(membIdArray) do
            local membObj = Doc():ObjectIdToObject(memberId);
            local dictValue = membObj:GetDoubleDictionary(0);
            session:DuplicateAndKeepObject(dictValue, memberId);
        end
        local pullSwPoint = session:GetKeepedObject(0):GetCenter();
        tool:AddHandle(0, false, pullSwPoint);
        tool:AddPolylineHandleRestriction(0, {pullSwPoint,{x=pullSwPoint.x, y=pullSwPoint.y + 10}});
    end
    
    OnStartHandleMoving = function(session, tool, handleId, objId)
        tool:HookObject(0, session:GetKeepedObject(0));
        tool:HookObject(1, session:GetKeepedObject(1));  -- [*1]
    end
    
    OnHandleMoving = function(session, tool, handleId, objId)
        if handleId == 0 then
            local point = tool:GetHandlePoint(handleId);
            session:GetKeepedObject(0):SetCenter(point);
            -- [*2]
            local jointArray = session:GetKeepedObject(1):AsObjPoly():GetJointArray();
            jointArray[2].center.y = point.y;
            session:GetKeepedObject(1):AsObjPoly():SetJointArray(jointArray);
        end
    end
    					
    Code[*1] adds the copy of the polyline-object[B] to the drawing target list.
    Code[*2] moves the end-edge of the the copy of the polyline-object[B] and draw its shape.
  28. The polyline-object[B] will follow the handle, in addition to the arc-object[A].
  29. Activate [Script Debugger] and add the code [*1], then save it.
    
    ResetHandles = function(session, tool, objId)
        local chip = Doc():ObjectIdToObject(objId);
        local membIdArray = chip:GetMemberIdArray();
        for i,memberId in ipairs(membIdArray) do
            local membObj = Doc():ObjectIdToObject(memberId);
            local dictValue = membObj:GetDoubleDictionary(0);
            session:DuplicateAndKeepObject(dictValue, memberId);
        end
        local pullSwPoint = session:GetKeepedObject(0):GetCenter();
        tool:AddHandle(0, false, pullSwPoint);
        tool:AddPolylineHandleRestriction(0, {pullSwPoint,{x=pullSwPoint.x, y=pullSwPoint.y + 10}});
    end
    
    OnStartHandleMoving = function(session, tool, handleId, objId)
        tool:HookObject(0, session:GetKeepedObject(0));
        tool:HookObject(1, session:GetKeepedObject(1));
    end
    
    OnHandleMoving = function(session, tool, handleId, objId)
        if handleId == 0 then
            local point = tool:GetHandlePoint(handleId);
            session:GetKeepedObject(0):SetCenter(point);
            local jointArray = session:GetKeepedObject(1):AsObjPoly():GetJointArray();
            jointArray[2].center.y = point.y;
            session:GetKeepedObject(1):AsObjPoly():SetJointArray(jointArray);
        end
    end
    
    -- [*1]
    OnStopHandleMoving = function(session, tool, handleId, objId, molipRec, molipHitType)
        local brushType, regBrushId, backSColorId, foreSColorId, patternId = session:GetKeepedObject(2):AsObjFigure():GetBrush();
        if backSColorId == SColor.WHITE then
            local yellow = Env():GetNearestSColor(0xffffff00);
            session:GetKeepedObject(2):AsObjFigure():SetBrush(BrushType.SOLID, 0, yellow, 0, 0);
        else
            session:GetKeepedObject(2):AsObjFigure():SetBrush(BrushType.SOLID, 0, SColor.WHITE, 0, 0);
        end
        session:StartTransaction();
        session:ApplyKeepedObject(2);
        session:CommitTransaction();
    end
    					
    [OnStopHandleMoving] is a function that is called when you finish to drag the handle.
    Code [*1] gets the color of the brush from the arc-object[C], and changes the color of the brush.
  30. The color of the arc-object[C] will be changed, when you pull the handle down.
  31. Start [Chip Editor] and click the menu [File] > [Chip properties].
  32. Show [Chip properties] dialog and [Script] tab, then set 'Lighting' script.
    Save and exit [Chip Editor] and save the document.