Make a chip of lighting that includes script.
Turn on and turn off the light when you pull the switch.
-
Show [Environment setup] dialog.
-
Show [Interface] tab, then mark [Expert mode] and [Run script in debug mode] checkbox.
-
Right-click on [Chip table] and execute [Modify chip table] command.
-
Click [Add] button in [Modify chip table] dialog.
-
[Chip Editor] will start. Show [Environment setup] dialog and mark [Expert mode] checkbox.
-
Make a lamp picture.
-
[A]Arc-object, [B]Polyline-object, [C]Arc-object, [D]Polyline-object
-
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.
-
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 -
Choose [Composite chip mode], then register the chip and exit [Chip Editor].
-
Place the created chip on the canvas.
-
Show [Properties] dialog of the chip, then show [Script] tab. Click [Select] button.
-
[Modify script table] dialog will be displayed. Click [Add] button.
-
[Create/edit script] dialog will be displayed. Set 'Lighting' in [Script name].
-
Confirm that 'Lighting' is selected in [Modify script table] dialog.
-
Confirm that 'Lighting' is set in [Script] tab of [Properties] dialog.
-
Click the menu [View] > [Show script debugger].
-
[Script Debugger] will be displayed.
-
Click the menu [Edit] > [Edit Script].
-
It will become enabled to edit the script code.
-
Input the following code.
[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.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
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. -
Save the script.
-
Double-click the chip to activate the script-tool. A handle will appear at the point of the arc-object[A].
-
You can drag the handle to any place in the canvas.
Deactivate the script-tool and activate the select-tool.
-
Activate [Script Debugger] and add the code [*1], [*2] and [*3], then save it.
[OnStartHandleMoving] is a function that is called when you start to drag the handle.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
[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.
-
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.
-
Activate [Script Debugger] and add code [*1] and [*2]. Then save it.
Code[*1] adds the copy of the polyline-object[B] to the drawing target list.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[*2] moves the end-edge of the the copy of the polyline-object[B] and draw its shape.
-
The polyline-object[B] will follow the handle, in addition to the arc-object[A].
-
Activate [Script Debugger] and add the code [*1], then save it.
[OnStopHandleMoving] is a function that is called when you finish to drag the handle.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
Code [*1] gets the color of the brush from the arc-object[C], and changes the color of the brush.
-
The color of the arc-object[C] will be changed, when you pull the handle down.
-
Start [Chip Editor] and click the menu [File] > [Chip properties].
-
Show [Chip properties] dialog and [Script] tab, then set 'Lighting' script.
Save and exit [Chip Editor] and save the document.