Make Helix Curve in OpenCASCADE
Abstract. OpenCASCADE does not provide helix curve directly, but you can build a helix curve by the pcurve of a surface(curve on surface). When you understand the pcurve of a surface, you can make a helix curve easily. The paper first make a helix by Tcl in Draw Test Harness, then translate the Tcl script to OpenCASCADE C++ code.
Key Words. OpenCASCADE, Helix Curve, PCurve, Sweep, Spring
?
1. Introduction
螺旋線(xiàn)是實(shí)踐中常用到的曲線(xiàn),例如平頭螺絲釘?shù)耐饩壡€(xiàn)就是螺旋線(xiàn)。當(dāng)我們擰緊平頭螺絲釘時(shí),它的外緣曲線(xiàn)上的任一點(diǎn)M一方面繞螺絲釘?shù)妮S旋轉(zhuǎn),另一方面又沿平行于軸線(xiàn)的方向前進(jìn),點(diǎn)M就走出一段螺旋線(xiàn)。[1]
如果空間一點(diǎn)M在圓柱面x*x+y*y=a*a上以角速度ω繞z軸旋轉(zhuǎn),同時(shí)又以線(xiàn)速度υ沿平等于z軸正方向上升(其中ω,υ都是常數(shù)),那未點(diǎn)M構(gòu)成的圖形叫螺旋線(xiàn)。其參數(shù)方程為:
Figure 1.1 A Helix Curve
OpenCASCADE 中并沒(méi)有直接提供構(gòu)造螺旋線(xiàn)的類(lèi)和函數(shù),因此只有自己來(lái)構(gòu)造了,其中構(gòu)造的核心是要理解PCurve(曲面的參數(shù)曲線(xiàn))。本文先以Tcl腳本在Draw Test Harness中快速生成一個(gè)螺旋線(xiàn),再將相應(yīng)的Tcl腳本轉(zhuǎn)換成C++代碼。在理解Pcurve概念的基礎(chǔ)上來(lái)構(gòu)造螺旋線(xiàn)還是很簡(jiǎn)單的,甚至還可以擴(kuò)展 應(yīng)用。
2.Make Helix Curve
在OpenCASCADE提供的一個(gè)經(jīng)典例子:生成一個(gè)酒瓶中,就有螺旋線(xiàn)的應(yīng)用,即生成瓶口處的螺紋。當(dāng)時(shí)看這例子的時(shí)候也是沒(méi)有完全理解,究竟怎么生成的那個(gè)螺旋線(xiàn)?感謝lifenli的提醒,使我又重溫了一遍例子,頓時(shí)茅塞頓開(kāi),明白了pcurve的一個(gè)應(yīng)用。
由《OpenCASCADE BRep Format》[4]中可知,圓柱面的參數(shù)方程為:
假設(shè)當(dāng)你在參數(shù)空間[u,v]中創(chuàng)建一條二維曲線(xiàn)后,可根據(jù)這個(gè)二維曲線(xiàn)來(lái)計(jì)算對(duì)應(yīng)曲面上的三維曲線(xiàn)。根據(jù)二維曲線(xiàn)的不同定義,得到的結(jié)果如下:
條件 |
參數(shù)方程 |
參數(shù)曲線(xiàn) |
U=0 |
S(v)=P+r*cos(u)+vDz |
與Z軸平行的直線(xiàn) |
V=0 |
S(u)=P+r*(cos(u)*Dx+sin(u)*Dy) |
與XOY面平行的圓 |
U!=0?&&?V?!=?0 |
S(u,v)=P+r(cos(u)*Dx+sin(u)*Dy)+vDz |
螺旋線(xiàn) |
# # make helix curve in OpenCASCADE. # Shing Liu(eryar@163.com) # 2015-07-08 22:00 # pload MODELING VISUALIZATION cylinder aCylinder 6 line aLine2d 0 0 1 1 trim aSegment aLine2d 0 2 * pi mkedge aHelixEdge aSegment aCylinder 0 6 * pi vdisplay aHelixEdge
Figure 2.1 Make a helix by Tcl script
上述Tcl腳本可以很容易的轉(zhuǎn)換成C++代碼的,下面給出相應(yīng)的C++實(shí)現(xiàn),源碼如下所示:
#define WNT #include <gp_Lin2d.hxx> #include <GCE2d_MakeSegment.hxx> #include <Geom_CylindricalSurface.hxx> #include <BRepBuilderAPI_MakeEdge.hxx> #include <TopoDS_Edge.hxx> #include <BRepTools.hxx> #pragma comment(lib, "TKernel.lib") #pragma comment(lib, "TKMath.lib") #pragma comment(lib, "TKG3d.lib") #pragma comment(lib, "TKBRep.lib") #pragma comment(lib, "TKGeomBase.lib") #pragma comment(lib, "TKTopAlgo.lib") void makeHelix( void ) { Handle_Geom_CylindricalSurface aCylinder = new Geom_CylindricalSurface(gp::XOY(), 6.0 ); gp_Lin2d aLine2d(gp_Pnt2d( 0.0 , 0.0 ), gp_Dir2d( 1.0 , 1.0 )); Handle_Geom2d_TrimmedCurve aSegment = GCE2d_MakeSegment(aLine2d, 0.0 , M_PI * 2.0 ); TopoDS_Edge aHelixEdge = BRepBuilderAPI_MakeEdge(aSegment, aCylinder, 0.0 , 6.0 * M_PI).Edge(); BRepTools::Dump(aHelixEdge, std::cout); BRepTools::Write(aHelixEdge, " d:/helix.brep " ); } int main( int argc, char * argv[]) { makeHelix(); return 0 ; }
參數(shù)空間中pcurve的斜率決定了螺旋線(xiàn)的螺距pitch,當(dāng)其他參數(shù)不變,改變斜率后得到如下圖所示結(jié)果:
Figure 2.2 Different Pitch by different K
由圖可知,當(dāng)pcurve的斜率越小時(shí),得到的螺旋線(xiàn)的螺距也越小。修改pcurve的斜率只需要修改上述Tcl腳本中的aLine2d的斜率。
如當(dāng)斜率k=1時(shí)的pcurve為:
當(dāng)斜率k=1.0/5.0時(shí)的pcurve為:
當(dāng)斜率k=1.0/10.0時(shí)的pcurve為:
可以自己嘗試修改看看沒(méi)的斜率得到的不同螺旋線(xiàn)的螺距變化。
3.Spring: Sweep profile along helix
得到螺旋線(xiàn)后自然就想到能不能用一個(gè)圓沿著螺旋線(xiàn)來(lái)放樣,從而得到一個(gè)彈簧。下面還是用Tcl腳本在Draw Test Harness中嘗試一下,相應(yīng)的C++實(shí)現(xiàn)也是很容易找到相關(guān)的類(lèi)。
# # make helix curve in OpenCASCADE. # Shing Liu(eryar@163.com) # 2015-07-08 22:00 # pload MODELING VISUALIZATION cylinder aCylinder 6 line aLine2d 0 0 1 1 trim aSegment aLine2d 0 2 * pi mkedge aHelixEdge aSegment aCylinder 0 6 * pi # there is no curve 3d in the pcurve edge. mkedgecurve aHelixEdge 0.001 wire aHelixWire aHelixEdge circle profile 6 0 0 0 4 1 1 mkedge profile profile wire profile profile mkplane profile profile pipe aSpring aHelixWire profile vdisplay aSpring vsetmaterial aSpring steel vsetgradientbg 180 200 255 180 180 180 2 vsetdispmode 1 vzbufftrihedron # set ray tracing if { ! [catch {vrenderparams -raytrace -shadows -reflections -fsaa -rayDepth 5 }] } { vtextureenv on 1 }
Figure 3.1 Spring by sweep a circle along a helix path
當(dāng)將pcruve在圓錐面上生成三維曲線(xiàn)時(shí)就會(huì)得到類(lèi)似夏天的蚊香那樣螺旋形狀。同樣使用上述代碼,只是將圓柱面改成圓錐面得到:
Figure 3.2 Mosquito Coil
4.Conclusion
綜 上所述,常見(jiàn)的計(jì)算幾何造型書(shū)中講到曲線(xiàn)的參數(shù)方程都會(huì)以螺旋線(xiàn)為經(jīng)典例子,甚至是高等數(shù)學(xué)中也是一樣,由此可見(jiàn)螺旋線(xiàn)是很常見(jiàn)的一種曲線(xiàn)。但是occ中 并沒(méi)有直接提供螺旋線(xiàn)的幾何曲線(xiàn),只有通過(guò)pcurve來(lái)構(gòu)造了。所以理解pcurve后,才好理解make bottle例子中的瓶頸螺紋部分的代碼。
通過(guò)將一個(gè)輪廓沿著螺旋線(xiàn)掃掠可以得出很多有意思的模型。在使用sweep的過(guò)程中發(fā)現(xiàn)pcurve生成的邊Edge中并沒(méi)有三維幾何曲 線(xiàn),所以會(huì)導(dǎo)致算法失敗。最終發(fā)現(xiàn)occ提供了一個(gè)將pcurve生成的邊中生成出一個(gè)擬合三維幾何曲線(xiàn)的函數(shù) BRepLib::BuildCurve3d()。對(duì)于一些在曲面上的曲線(xiàn)的造型可以參考這種用法,用pcurve來(lái)構(gòu)造。
5. References
1. 同濟(jì)大學(xué)數(shù)學(xué)教研室. 高等數(shù)學(xué)(上). 高等教育出版社. 1978
2. Helix. http://mathworld.wolfram.com/Helix.html
3. OpenCASCADE Make Bottle Tutorial. 2015
4. OpenCASCADE BRep Format. 2015
5. 莫勇,常智勇. 計(jì)算機(jī)輔助幾何造型技術(shù). 科學(xué)出版社. 2009
?
PDF Version and Tcl Script Make Helix Curve in OpenCASCADE
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
