2015年8月27日星期四

初試用 OpenSCAD 劃 ActionCam 3D 架.

之前劃 3D Model 去印都係用 Sketchup 劃, 近兩日才認識這個 OpenSCAD  http://www.openscad.org/ 加上想做個 ActionCAM 立體拍片架, 所以就小試牛刀.

相比用 Sketchup. 寫 Script 對我來說相對容易同好玩.


OpenSCAD Model




這種做法是參考網上的 GoPro 3D.

OpenSCAD File:


module sjcambase() {
    difference() {
        cube([70,52,30]);

        translate([5,5,5]) {
            linear_extrude(26) {
                square([60,42]);
            }
        }
        
        translate([10+11,15+5+11,-1]) {
            linear_extrude(7) {
                circle(d=22);
            }
        }
        
        
        translate([6+5+43,6+5+23,-1]) {
            linear_extrude(7) {
                circle(d=12);
            }    
        }
        
        translate([8 + 5 + 40,5+42+5+1,8+5 + 6]) {
            rotate([90,0,0]) {
                linear_extrude(7) {
                    circle(d=16);
                }
            }
        }
        
        translate([5+60-1,5,5+13]) {
            cube([3,42, 8]);
        }
    }
}


difference() {
    union() {
        translate([70,52+(42-15-11-5-1),0]) {
            rotate([0,0,180]) {    
                sjcambase();
            }
        }
        
        translate([65,0,0]) {
            sjcambase();
        }
    }
    
    translate([65-1,11+4,5]) {
        cube([7,42-10, 26]);
    }
}

以上謹對應我手上的 ActionCAM. 別的 Action CAM 有需要修改大少及按鈕位置.

2013年3月25日星期一

試用 Expression 於執得時生產 Function

大部份時間只會用 LINQ Expression 去進行 Data Query. 今日試下用 Expression 去生產 Function.

下面係今日試制的代碼:

public void Test()
{
  var Param1 = Expression.Parameter(typeof(String), "a");
  var Method1 = typeof(String).GetMethod("Trim", new Type[] { });
  var CallMethod1 = Expression.Call(Param1, Method1);
  Expression> Ex;
  Ex = Expression.Lambda>(CallMethod1, Param1);
  String Msg = " ABC ";
  String Result = Ex.Compile().Invoke(Msg);
}

以上代碼會產生一句類似 a=>a.Trim() 的 LINQ Expression 再 Compile 成 Function

Msg = "ABC"
Result = "ABC";

var Param1.... 創建一個 LINQ Parameter "a"
var Method1.... 用 Reflection 去找找 String 的 Trim() Method.
var CallMethod1.... 創建 LINQ Expression 的 Body "a.Trim()"

Ex = Expression.Lambda..... 用 Parameter "a" 同 a.Trim() 建立 LINQ Expression "a=>a.Trim()"
最後 Ex.Compile() 就會成 Func 而非 Expression>.

Invoke 一次就可以將 Msg 進行 Trim() 的動作.

這裡可以用 Func 這個 Type 去記住動態生產出來的 Function 不用次次進行 Compile.

當然我會更懶地用 Dynamic Language Runtime 進行以下動作.

dynamic MethodObj = new ExpandoObject();
MethodObj.CustomMethod1 = Ex.Compile();
String Result = MethodObj.CustomMethod1(Msg);

這樣就可以直接調用 MethodObj 的 CustomMethod1 就進行 Trim 的動作.

當然 Trim 又可需要這麼煩的動作. 這只是一個最簡單運用 Expression 進行生產代碼的方法.