「こんにちは、お正月」たたはVisual Basic .NETを䜿甚しおNanoCADをプログラミングしたす

先日 SDKに付属しおいたNanoCAD APIのドキュメントを参照しおいるずきに、.NET APIずMultiCAD.NET APIのクラスメンバヌの説明がCずVisual Basicの䞡方で提䟛されおいるこずに突然気付きたした。 そしお、私は考えたした「しかし、VBの説明があるのは玠晎らしいこずです」



正盎に蚀っお、私はVBをたったく知りたせんが、100幎前に叀き良きBASICでコヌドを芋たずき、それは人々がプログラミングの基瀎を孊び始める蚀語の1぀なので、プログラムの普及に少し貢献するこずにしたした。



NanobrでのVBの䜿甚に関するHabrに関する良い蚘事が既にあるず蚀わなければなりたせん。NanoCADずExcelの組み合わせず、それが最終的に建築蚭蚈を容易にする方法に぀いお説明しおいたす。



別の、よりシンプルで、よりお祝いのタスクを解決し、クリスマスツリヌを描いお 、ナヌザヌに新幎をお祈りしたす。 この蚘事はVBに専念しおいるずいう事実にもかかわらず、 Cのコヌドも同様になりたす。



たた、「新幎」は費甚のかかる䌑日であるため、 NanoCAD 5.1の商甚利甚の無料版に焊点を圓おたすただし、理論的にはNC 8.Xでは問題なく動䜜するはずです。



たた、MonoずWineを䜿甚したCコヌドをコンパむルしお実行できるため、Linuxナヌザヌをバむパスしたせん。



正盎に蚀うず、私自身が最近NanoCAD APIを習埗し始めたので、今幎の最埌の䌑日の蚘事は、コヌドの耇雑さの暖かいチュヌブグラフィックアヌティスト「KengurenokROO」を思い出させたす。







PSこれは「Habrahabr」ずいう蚀葉の最初の文字です-これ以䞊は十分ではありたせんでした=





私の新しい趣味はすでに小さなシリヌズになっおいるので、念のため、ネタバレの䞋に以前のすべおの蚘事ぞのリンクを掲茉したす。





はじめに、い぀ものように、 私はプログラマヌではないため、この蚘事の私の考えがすべお100正しいわけではないこず、たた、 私がロシアCADを普及させるずいうむデオロギヌの理由から、NanoCAD開発者ずは䜕の関係もないこずを思い出したす。



幎末には倧量のテキストを䜜成したくないので、蚘事は短くなりたす。それを分析したす。



内容

パヌトIはじめに

パヌトIICコヌドの蚘述

パヌトIIIVBでのコヌドの蚘述

パヌトIV来お



パヌトIICコヌドの蚘述



NC 8.5のVisual Studio 2015で新しいプロゞェクトを䜜成する方法



今回は、奜奇心のために、より軜量のIDE Xamarin Studio MonoDevelop でプロゞェクトを䜜成したしょう。



プロセス党䜓をネタバレの䞋に隠したす。



Xamarin Studio IDEでNanoCAD 5.1のCプロゞェクトを䜜成する方法
たず、新しいプロゞェクトを䜜成し、Cクラスラむブラリを遞択したす。







圌に名前を぀けお







次に、RMBでプロゞェクトをクリックし、「パラメヌタヌ」ボタンを遞択しお、プロゞェクトを構成したす。







.Net 3.5バヌゞョンを遞択







次に、デバッグの䟿宜䞊、「F5」キヌを抌しおNanocadの起動を蚭定したす

コヌドを倉曎するたびに、Nanocadを完党に再起動する必芁がありたす







次に、図の右偎に衚瀺されおいるラむブラリが必芁なラむブラリぞのリンクをロヌドしたす







「ロヌカルにコピヌ」項目からmapimgdラむブラリのチェックを倖すこずを忘れないでください







実は、F5を抌しおすべおが開始するこずを確認するコヌドを蚘述するこずは実際には残っおいたす。







ラむブラリをロヌドするには、NanoCADコン゜ヌルでNetloadコマンドを入力し、dllを遞択したす通垞はプロゞェクトフォルダヌにありたす。たずえば、C\ Users \ b \ Dev \ habr \ XMTree \ XMTree \ bin \ Debug



毎回これをしないために

アドレスCに移動したす\ ProgramData \ Nanosoft \ nanoCAD 5.1 \ DataRW倉曎される堎合がありたすおよび次の内容のload.configファむルを怜玢たたは䜜成したす



<root> <list> <module path="C:\Users\...\bin\Debug\ .dll"/> </list> </root>
      
      





その埌、プログラムの起動時にラむブラリが自動的にロヌドされたす。



Linuxでも同様の操䜜を実行できたす。Linuxでは、NanoCAD 5.1を実行するにはWineを、プログラミングにはMonoDevelopが必芁です。 前回の蚘事で詳しく説明したした 。



残念ながら、Wineで問題が発生し、NanoCADの色がどこかで壊れおいたため、Linuxツリヌは次のようになりたす。



Linuxでのクリスマスツリヌ






Cコヌドを詳现に分解するこずはせず、実際には新しいこずは䜕もしたせん。



぀たり、ポリラむンを描画しおストロヌクを䜜成するチヌムを䜜成したす。

スポむラヌの䞋ずGitHubのコヌドVB甚のバヌゞョンもありたす。



CのNC 5.1の完党なコヌド
 //Use Microsoft .NET Framework 3.5 and old version of MultiCad.NET (for NC 5.1) //Class for demonstrating the capabilities of MultiCad.NET //Assembly for the Nanocad 5.1 //Link mapimgd from Nanocad SDK //Link System.Windows.Forms and System.Drawing //The commands: draws a christmas three. //This code in the part of non-infringing rights Nanosoft can be used and distributed in any accessible ways. //For the consequences of the code application, the developer is not responsible. //The code was not tested with NANOCAD 8.X. // However, it should work, if you update SDK libraries and include NC 8.X dll using System.Collections.Generic; using System.Linq; using Multicad.Runtime; using Multicad.DatabaseServices; using Multicad.Geometry; using Multicad.DatabaseServices.StandardObjects; using System.Drawing; using System.Windows.Forms; namespace XmasThree { class XmasThree { [CommandMethod("DXThree", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void DrawXThree() { Point3d _pntBase; Point3d _bufPnt; //prompts for installation point entry InputJig jig = new InputJig(); // Get the first box point from the jig InputResult res = jig.GetPoint("Select first point:"); //It works only if input was successful if (res.Result == InputResult.ResultCode.Normal) { // The base point is taken from the entry point (click with mouse) _pntBase = res.Point; //Draw the outline of the left half of the Christmas tree //Create base points for the polyline List<Point3d> leftPatrOfThreePoints = new List<Point3d>() { new Point3d(_pntBase.X, _pntBase.Y, 0), new Point3d(_pntBase.X-125, _pntBase.Y-154, 0), new Point3d(_pntBase.X-31, _pntBase.Y-137, 0), new Point3d(_pntBase.X-181, _pntBase.Y-287, 0), new Point3d(_pntBase.X-31, _pntBase.Y-253, 0), new Point3d(_pntBase.X-242, _pntBase.Y-400, 0), new Point3d(_pntBase.X-37, _pntBase.Y-400, 0), new Point3d(_pntBase.X-37, _pntBase.Y-454, 0), new Point3d(_pntBase.X, _pntBase.Y-454, 0) }; //Create a polyline (geometry) Polyline3d leftPatrOfThree = new Polyline3d(leftPatrOfThreePoints); //Create a polyline object and place it on the drawing DbPolyline XThreeLeft = new DbPolyline(); XThreeLeft.Polyline = new Polyline3d(leftPatrOfThree); XThreeLeft.Polyline.SetClosed(false); XThreeLeft.DbEntity.Color = Color.Green; XThreeLeft.DbEntity.AddToCurrentDocument(); //The right part of the tree is obtained by mirroring the left DbPolyline XThreeRight = new DbPolyline(); XThreeRight.DbEntity.Color = Color.Green; XThreeRight.Polyline = (Polyline3d)XThreeLeft.Polyline.Mirror(new Plane3d(_pntBase, new Vector3d(10, 0, 0))); XThreeRight.DbEntity.AddToCurrentDocument(); //From the right and left sides we make a single contour for hatching DbPolyline XThreeR = new DbPolyline(); XThreeR.DbEntity.Color = Color.Green; //XThreeR.Polyline = XThreeRight.Polyline.Clone() as Polyline3d; for NC 8.5 XThreeR.Polyline = XThreeRight.Polyline.GetCopy() as Polyline3d; //FOR NC 5.1 XThreeR.DbEntity.AddToCurrentDocument(); List<Point3d> hatchPoints = new List<Point3d>(); hatchPoints.AddRange(leftPatrOfThreePoints); hatchPoints.AddRange(XThreeR.Polyline.Points.Reverse().ToList()); Polyline3d hatchContur = new Polyline3d(hatchPoints); //We will create on the basis of a contour a hatch (geometry) with continuous filling Hatch hatch = new Hatch(hatchContur, 0, 10, true); hatch.PattType = PatternType.PreDefined; hatch.PatternName = "SOLID"; //Based on the geometry of the hatch, we create the document object, set its color properties - green DbGeometry dbhatch = new DbGeometry(); dbhatch.Geometry = new EntityGeometry(hatch); dbhatch.DbEntity.Color = Color.Green; dbhatch.DbEntity.AddToCurrentDocument(); // if you want you can try to draw balls with circles use // DrawThreeBalls(_pntBase); //Similarly, make a Christmas tree toy (octagon) //red _bufPnt = _pntBase.Subtract(new Vector3d(30, 95, 0)); DbPolyline dbOctoRed = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoRed.DbEntity.AddToCurrentDocument(); Hatch hatchCirkRed = new Hatch(dbOctoRed.Polyline, 0, 1, false); hatchCirkRed.PattType = PatternType.PreDefined; hatchCirkRed.PatternName = "SOLID"; DbGeometry dbhatchCirkRed = new DbGeometry(); dbhatchCirkRed.Geometry = new EntityGeometry(hatchCirkRed); dbhatchCirkRed.DbEntity.Color = Color.Red; dbhatchCirkRed.DbEntity.AddToCurrentDocument(); //green _bufPnt = _pntBase.Subtract(new Vector3d(-40, 200, 0)); DbPolyline dbOctoGreen = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoGreen.DbEntity.AddToCurrentDocument(); Hatch hatchCirkGreen = new Hatch(dbOctoGreen.Polyline, 0, 1, false); hatchCirkGreen.PattType = PatternType.PreDefined; hatchCirkGreen.PatternName = "SOLID"; DbGeometry dbhatchCirkGreen = new DbGeometry(); dbhatchCirkGreen.Geometry = new EntityGeometry(hatchCirkGreen); dbhatchCirkGreen.DbEntity.Color = Color.LightSeaGreen; dbhatchCirkGreen.DbEntity.AddToCurrentDocument(); //blue _bufPnt = _pntBase.Subtract(new Vector3d(-12, 350, 0)); DbPolyline dbOctoBlue = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoBlue.DbEntity.AddToCurrentDocument(); Hatch hatchCirkBlue = new Hatch(dbOctoBlue.Polyline, 0, 1, false); hatchCirkBlue.PattType = PatternType.PreDefined; hatchCirkBlue.PatternName = "SOLID"; DbGeometry dbhatchCirkBlue = new DbGeometry(); dbhatchCirkBlue.Geometry = new EntityGeometry(hatchCirkBlue); dbhatchCirkBlue.DbEntity.Color = Color.Blue; dbhatchCirkBlue.DbEntity.AddToCurrentDocument(); //display the text with congratulations MessageBox.Show("I Wish You A Merry Christmas And Happy New Year!"); } } public Polyline3d DrawThreeOctogonPl(Point3d _pntB) { //Create points for an octagon List<Point3d> octoPoints = new List<Point3d>() { new Point3d(_pntB.X, _pntB.Y, 0), new Point3d(_pntB.X-15, _pntB.Y, 0), new Point3d(_pntB.X-25, _pntB.Y-11.3, 0), new Point3d(_pntB.X-25, _pntB.Y-26.3, 0), new Point3d(_pntB.X-15, _pntB.Y-37.6, 0), new Point3d(_pntB.X, _pntB.Y-37.6, 0), new Point3d(_pntB.X+9.7, _pntB.Y-26.3, 0), new Point3d(_pntB.X+9.7, _pntB.Y-11.3, 0), new Point3d(_pntB.X, _pntB.Y, 0) }; return new Polyline3d(octoPoints); } //Draws three balls instead of an octagon, can not earn in NanoCAD 8.X public void DrawThreeBalls(Point3d _pntB) { CircArc3d circarcRed = new CircArc3d(_pntB.Subtract(new Vector3d(30, 100, 0)), Vector3d.ZAxis, 15); DbCircArc dbCircarcRed = circarcRed;//implicit dbCircarcRed.DbEntity.AddToCurrentDocument(); Hatch hatchCirkRed = new Hatch(circarcRed, 0, 1, false); hatchCirkRed.PattType = PatternType.PreDefined; hatchCirkRed.PatternName = "SOLID"; DbGeometry dbhatchCirkRed = new DbGeometry(); dbhatchCirkRed.Geometry = new EntityGeometry(hatchCirkRed); dbhatchCirkRed.DbEntity.Color = Color.Red; dbhatchCirkRed.DbEntity.AddToCurrentDocument(); CircArc3d circarcGreen = new CircArc3d(_pntB.Subtract(new Vector3d(-40, 200, 0)), Vector3d.ZAxis, 15); DbCircArc dbCircarcGreen = circarcGreen;//implicit dbCircarcGreen.DbEntity.AddToCurrentDocument(); Hatch hatchCirkGreen = new Hatch(circarcGreen, 0, 1, false); hatchCirkGreen.PattType = PatternType.PreDefined; hatchCirkGreen.PatternName = "SOLID"; DbGeometry dbhatchCirkGreen = new DbGeometry(); dbhatchCirkGreen.Geometry = new EntityGeometry(hatchCirkGreen); dbhatchCirkGreen.DbEntity.Color = Color.LightSeaGreen; dbhatchCirkGreen.DbEntity.AddToCurrentDocument(); CircArc3d circarcBlue = new CircArc3d(_pntB.Subtract(new Vector3d(-12, 350, 0)), Vector3d.ZAxis, 15); DbCircArc dbCircarcBlue = circarcBlue;//implicit dbCircarcBlue.DbEntity.AddToCurrentDocument(); Hatch hatchCirkBlue = new Hatch(circarcBlue, 0, 1, false); hatchCirkBlue.PattType = PatternType.PreDefined; hatchCirkBlue.PatternName = "SOLID"; DbGeometry dbhatchCirkBlue = new DbGeometry(); dbhatchCirkBlue.Geometry = new EntityGeometry(hatchCirkBlue); dbhatchCirkBlue.DbEntity.Color = Color.Blue; dbhatchCirkBlue.DbEntity.AddToCurrentDocument(); } } }
      
      





理論䞊、このコヌドはNC 8.Xでも動䜜するはずです。察応するビット容量x64たたはx86のむンクルヌドフォルダヌからプロゞェクトにmapibasetypes.dllおよびmapimgd.dllを含めるために.NET Framework 4.0を遞択し、1行察応するコメントがある堎合たた、DrawThreeBallsメ゜ッドをコメント化たたは削陀したす



次のようになりたす。



CのNC 8.Xの完党なコヌド
 //Use Microsoft .NET Framework 4 and old version of MultiCad.NET (for NC 8.X) //Class for demonstrating the capabilities of MultiCad.NET //Assembly for the Nanocad 8.X //Link mapimgd and mapibasetypes from Nanocad SDK //Link System.Windows.Forms and System.Drawing //The commands: draws a christmas three. //This code in the part of non-infringing rights Nanosoft can be used and distributed in any accessible ways. //For the consequences of the code application, the developer is not responsible. using System.Collections.Generic; using System.Linq; using Multicad.Runtime; using Multicad.DatabaseServices; using Multicad.Geometry; using Multicad.DatabaseServices.StandardObjects; using System.Drawing; using System.Windows.Forms; namespace XmasThree { class XmasThree { [CommandMethod("DXThree", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void DrawXThree() { Point3d _pntBase; Point3d _bufPnt; //prompts for installation point entry InputJig jig = new InputJig(); // Get the first box point from the jig InputResult res = jig.GetPoint("Select first point:"); //It works only if input was successful if (res.Result == InputResult.ResultCode.Normal) { // The base point is taken from the entry point (click with mouse) _pntBase = res.Point; //Draw the outline of the left half of the Christmas tree //Create base points for the polyline List<Point3d> leftPatrOfThreePoints = new List<Point3d>() { new Point3d(_pntBase.X, _pntBase.Y, 0), new Point3d(_pntBase.X-125, _pntBase.Y-154, 0), new Point3d(_pntBase.X-31, _pntBase.Y-137, 0), new Point3d(_pntBase.X-181, _pntBase.Y-287, 0), new Point3d(_pntBase.X-31, _pntBase.Y-253, 0), new Point3d(_pntBase.X-242, _pntBase.Y-400, 0), new Point3d(_pntBase.X-37, _pntBase.Y-400, 0), new Point3d(_pntBase.X-37, _pntBase.Y-454, 0), new Point3d(_pntBase.X, _pntBase.Y-454, 0) }; //Create a polyline (geometry) Polyline3d leftPatrOfThree = new Polyline3d(leftPatrOfThreePoints); //Create a polyline object and place it on the drawing DbPolyline XThreeLeft = new DbPolyline(); XThreeLeft.Polyline = new Polyline3d(leftPatrOfThree); XThreeLeft.Polyline.SetClosed(false); XThreeLeft.DbEntity.Color = Color.Green; XThreeLeft.DbEntity.AddToCurrentDocument(); //The right part of the tree is obtained by mirroring the left DbPolyline XThreeRight = new DbPolyline(); XThreeRight.DbEntity.Color = Color.Green; XThreeRight.Polyline = (Polyline3d)XThreeLeft.Polyline.Mirror(new Plane3d(_pntBase, new Vector3d(10, 0, 0))); XThreeRight.DbEntity.AddToCurrentDocument(); //From the right and left sides we make a single contour for hatching DbPolyline XThreeR = new DbPolyline(); XThreeR.DbEntity.Color = Color.Green; XThreeR.Polyline = XThreeRight.Polyline.Clone() as Polyline3d; // for NC 8.5 //XThreeR.Polyline = XThreeRight.Polyline.GetCopy() as Polyline3d; //FOR NC 5.1 XThreeR.DbEntity.AddToCurrentDocument(); List<Point3d> hatchPoints = new List<Point3d>(); hatchPoints.AddRange(leftPatrOfThreePoints); hatchPoints.AddRange(XThreeR.Polyline.Points.Reverse().ToList()); Polyline3d hatchContur = new Polyline3d(hatchPoints); //We will create on the basis of a contour a hatch (geometry) with continuous filling Hatch hatch = new Hatch(hatchContur, 0, 10, true); hatch.PattType = PatternType.PreDefined; hatch.PatternName = "SOLID"; //Based on the geometry of the hatch, we create the document object, set its color properties - green DbGeometry dbhatch = new DbGeometry(); dbhatch.Geometry = new EntityGeometry(hatch); dbhatch.DbEntity.Color = Color.Green; dbhatch.DbEntity.AddToCurrentDocument(); // if you want you can try to draw balls with circles use // DrawThreeBalls(_pntBase); //Similarly, make a Christmas tree toy (octagon) //red _bufPnt = _pntBase.Subtract(new Vector3d(30, 95, 0)); DbPolyline dbOctoRed = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoRed.DbEntity.AddToCurrentDocument(); Hatch hatchCirkRed = new Hatch(dbOctoRed.Polyline, 0, 1, false); hatchCirkRed.PattType = PatternType.PreDefined; hatchCirkRed.PatternName = "SOLID"; DbGeometry dbhatchCirkRed = new DbGeometry(); dbhatchCirkRed.Geometry = new EntityGeometry(hatchCirkRed); dbhatchCirkRed.DbEntity.Color = Color.Red; dbhatchCirkRed.DbEntity.AddToCurrentDocument(); //green _bufPnt = _pntBase.Subtract(new Vector3d(-40, 200, 0)); DbPolyline dbOctoGreen = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoGreen.DbEntity.AddToCurrentDocument(); Hatch hatchCirkGreen = new Hatch(dbOctoGreen.Polyline, 0, 1, false); hatchCirkGreen.PattType = PatternType.PreDefined; hatchCirkGreen.PatternName = "SOLID"; DbGeometry dbhatchCirkGreen = new DbGeometry(); dbhatchCirkGreen.Geometry = new EntityGeometry(hatchCirkGreen); dbhatchCirkGreen.DbEntity.Color = Color.LightSeaGreen; dbhatchCirkGreen.DbEntity.AddToCurrentDocument(); //blue _bufPnt = _pntBase.Subtract(new Vector3d(-12, 350, 0)); DbPolyline dbOctoBlue = DrawThreeOctogonPl(_bufPnt);//implicit dbOctoBlue.DbEntity.AddToCurrentDocument(); Hatch hatchCirkBlue = new Hatch(dbOctoBlue.Polyline, 0, 1, false); hatchCirkBlue.PattType = PatternType.PreDefined; hatchCirkBlue.PatternName = "SOLID"; DbGeometry dbhatchCirkBlue = new DbGeometry(); dbhatchCirkBlue.Geometry = new EntityGeometry(hatchCirkBlue); dbhatchCirkBlue.DbEntity.Color = Color.Blue; dbhatchCirkBlue.DbEntity.AddToCurrentDocument(); //display the text with congratulations MessageBox.Show("I Wish You A Merry Christmas And Happy New Year!"); } } public Polyline3d DrawThreeOctogonPl(Point3d _pntB) { //Create points for an octagon List<Point3d> octoPoints = new List<Point3d>() { new Point3d(_pntB.X, _pntB.Y, 0), new Point3d(_pntB.X-15, _pntB.Y, 0), new Point3d(_pntB.X-25, _pntB.Y-11.3, 0), new Point3d(_pntB.X-25, _pntB.Y-26.3, 0), new Point3d(_pntB.X-15, _pntB.Y-37.6, 0), new Point3d(_pntB.X, _pntB.Y-37.6, 0), new Point3d(_pntB.X+9.7, _pntB.Y-26.3, 0), new Point3d(_pntB.X+9.7, _pntB.Y-11.3, 0), new Point3d(_pntB.X, _pntB.Y, 0) }; return new Polyline3d(octoPoints); } } }
      
      







パヌトIIIVBでのコヌドの蚘述



Visual Studio 2015 for VB.NETでプロゞェクトを䜜成するプロセスは、Cのプロゞェクトを䜜成するプロセスずほが完党に同じですが、必芁なスキルを持っおいない人でも蚘事を読むこずができるので、この点をさらに詳しく分析したす。



たず、新しいプロゞェクトを䜜成し、VB.NETクラスラむブラリを遞択し、.NET Framework 3.5NC 8.Xバヌゞョン4.0甚を遞択したす







次に、右偎の「リンク」ボタンを右クリックしお、NanoCADのすべおのバヌゞョンのSDKでむンストヌルしたラむブラリにリンクを远加したす。



このプロゞェクトでは、mapimgdだけで十分ですNC 8.5ではmapibasetypesも必芁です。

たた、暙準のSystem.Windows.FormsずSystem.Drawingをロヌドする必芁がありたす







「ロヌカルにコピヌ」プロパティのチェックを倖すこずを忘れないでください







次に、RMBでプロゞェクトをクリックし、「プロパティ」ボタンを遞択しお、プロゞェクトを蚭定したす

デバッグの䟿宜䞊、「F5」キヌを抌しおNanocadの起動を蚭定したす







コヌドを倉曎するたびに、Nanocadを完党に再起動する必芁がありたす



ラむブラリをロヌドするには、NanoCADコン゜ヌルでNetloadコマンドを入力し、コンパむル枈みのdllを遞択したす通垞はプロゞェクトフォルダヌにありたす。たずえば、C\ Users \ b \ Dev \ habr \ XMTree \ XMTree \ bin \ Debug。



毎回これを行わないために、アドレスC\ ProgramData \ Nanosoft \ nanoCAD 5.1 \ DataRWこれは異なる堎合がありたすに移動し、次の内容のload.configファむルを怜玢たたは䜜成したす。



 <root> <list> <module path="C:\Users\...\bin\Debug\ .dll"/> </list> </root>
      
      





その埌、プログラムの起動時にラむブラリが自動的にロヌドされたす。



すでに蚀ったように、VB.NETに぀いおはたったく知りたせんが、Cを少し知っおいお、CからVBぞのコヌドコンバヌタヌにすべおの倧たかな䜜業たずえば、 これ を実行させるず、必芁なのは、少し修正するこずだけです。



完党なコマンドコヌドをスポむラヌの䞋に配眮し、それを郚分的に分析したす。



VB.NET䞊のNC 5.1の完党なコヌド
 Imports System.Collections.Generic Imports Multicad.Runtime Imports Multicad.DatabaseServices Imports Multicad.Geometry Imports Multicad.DatabaseServices.StandardObjects Imports System.Drawing Imports System.Windows.Forms Namespace XmasThree Class XmasThree <CommandMethod("DXThree", CommandFlags.NoCheck Or CommandFlags.NoPrefix)> Public Sub DrawXThree() Dim _pntBase As Point3d Dim _bufPnt As Point3d Dim jig As New InputJig() 'prompts for installation point entry Dim res As InputResult = jig.GetPoint("Select first point:") If res.Result = InputResult.ResultCode.Normal Then 'The base point is taken from the entry point (click with mouse) _pntBase = res.Point 'Draw the outline of the left half of the Christmas tree 'Create base points for the polyline Dim leftPatrOfThreePoints As New List(Of Point3d)() From { New Point3d(_pntBase.X, _pntBase.Y, 0), New Point3d(_pntBase.X - 125, _pntBase.Y - 154, 0), New Point3d(_pntBase.X - 31, _pntBase.Y - 137, 0), New Point3d(_pntBase.X - 181, _pntBase.Y - 287, 0), New Point3d(_pntBase.X - 31, _pntBase.Y - 253, 0), New Point3d(_pntBase.X - 242, _pntBase.Y - 400, 0), New Point3d(_pntBase.X - 37, _pntBase.Y - 400, 0), New Point3d(_pntBase.X - 37, _pntBase.Y - 454, 0), New Point3d(_pntBase.X, _pntBase.Y - 454, 0) } 'Create a polyline (geometry) Dim leftPatrOfThree As New Polyline3d(leftPatrOfThreePoints) 'Create a polyline object and place it on the drawing Dim XThreeLeft As New DbPolyline() XThreeLeft.Polyline = New Polyline3d(leftPatrOfThree) XThreeLeft.Polyline.SetClosed(False) XThreeLeft.DbEntity.Color = Color.Green XThreeLeft.DbEntity.AddToCurrentDocument() Dim XThreeRight As New DbPolyline() XThreeRight.DbEntity.Color = Color.Green XThreeRight.Polyline = DirectCast(XThreeLeft.Polyline.Mirror(New Plane3d(_pntBase, New Vector3d(10, 0, 0))), Polyline3d) XThreeRight.DbEntity.AddToCurrentDocument() 'From the right and left sides we make a single contour for hatching Dim XThreeR As New DbPolyline() XThreeR.DbEntity.Color = Color.Green XThreeR.Polyline = TryCast(XThreeRight.Polyline.GetCopy(), Polyline3d) XThreeR.DbEntity.AddToCurrentDocument() Dim hatchPoints As New List(Of Point3d)() hatchPoints.AddRange(leftPatrOfThreePoints) hatchPoints.AddRange(XThreeR.Polyline.Points.Reverse().ToList()) Dim hatchContur As New Polyline3d(hatchPoints) 'We will create on the basis of a contour a hatch (geometry) with continuous filling Dim hatch As New Hatch(hatchContur, 0, 10, True) hatch.PattType = PatternType.PreDefined hatch.PatternName = "SOLID" 'Based on the geometry of the hatch, we create the document object, set its color properties - green Dim dbhatch As New DbGeometry() dbhatch.Geometry = New EntityGeometry(hatch) dbhatch.DbEntity.Color = Color.Green dbhatch.DbEntity.AddToCurrentDocument() 'Similarly, make a Christmas tree toy (octagon) 'red _bufPnt = _pntBase.Subtract(New Vector3d(30, 95, 0)) Dim dbOctoRed As DbPolyline = DrawThreeOctogonPl(_bufPnt) dbOctoRed.DbEntity.AddToCurrentDocument() Dim hatchCirkRed As New Hatch(dbOctoRed.Polyline, 0, 1, False) hatchCirkRed.PattType = PatternType.PreDefined hatchCirkRed.PatternName = "SOLID" Dim dbhatchCirkRed As New DbGeometry() dbhatchCirkRed.Geometry = New EntityGeometry(hatchCirkRed) dbhatchCirkRed.DbEntity.Color = Color.Red dbhatchCirkRed.DbEntity.AddToCurrentDocument() 'green _bufPnt = _pntBase.Subtract(New Vector3d(-40, 200, 0)) Dim dbOctoGreen As DbPolyline = DrawThreeOctogonPl(_bufPnt) dbOctoGreen.DbEntity.AddToCurrentDocument() Dim hatchCirkGreen As New Hatch(dbOctoGreen.Polyline, 0, 1, False) hatchCirkGreen.PattType = PatternType.PreDefined hatchCirkGreen.PatternName = "SOLID" Dim dbhatchCirkGreen As New DbGeometry() dbhatchCirkGreen.Geometry = New EntityGeometry(hatchCirkGreen) dbhatchCirkGreen.DbEntity.Color = Color.LightSeaGreen dbhatchCirkGreen.DbEntity.AddToCurrentDocument() 'blue _bufPnt = _pntBase.Subtract(New Vector3d(-12, 350, 0)) Dim dbOctoBlue As DbPolyline = DrawThreeOctogonPl(_bufPnt) dbOctoBlue.DbEntity.AddToCurrentDocument() Dim hatchCirkBlue As New Hatch(dbOctoBlue.Polyline, 0, 1, False) hatchCirkBlue.PattType = PatternType.PreDefined hatchCirkBlue.PatternName = "SOLID" Dim dbhatchCirkBlue As New DbGeometry() dbhatchCirkBlue.Geometry = New EntityGeometry(hatchCirkBlue) dbhatchCirkBlue.DbEntity.Color = Color.Blue dbhatchCirkBlue.DbEntity.AddToCurrentDocument() MessageBox.Show("I Wish You A Merry Christmas And Happy New Year!!!") End If End Sub Public Function DrawThreeOctogonPl(_pntB As Point3d) As Polyline3d 'Create points for an octagon Dim octoPoints As New List(Of Point3d)() From { New Point3d(_pntB.X, _pntB.Y, 0), New Point3d(_pntB.X - 15, _pntB.Y, 0), New Point3d(_pntB.X - 25, _pntB.Y - 11.3, 0), New Point3d(_pntB.X - 25, _pntB.Y - 26.3, 0), New Point3d(_pntB.X - 15, _pntB.Y - 37.6, 0), New Point3d(_pntB.X, _pntB.Y - 37.6, 0), New Point3d(_pntB.X + 9.7, _pntB.Y - 26.3, 0), New Point3d(_pntB.X + 9.7, _pntB.Y - 11.3, 0), New Point3d(_pntB.X, _pntB.Y, 0) } Return New Polyline3d(octoPoints) End Function End Class End Namespace
      
      





コヌドは単玔ですが、念のため、VBの堎合はこれたで行ったこずがないため、コヌドを詳现か぀郚分的に分析したす。



 Imports System.Collections.Generic Imports Multicad.Runtime Imports Multicad.DatabaseServices Imports Multicad.Geometry Imports Multicad.DatabaseServices.StandardObjects Imports System.Drawing Imports System.Windows.Forms
      
      





描画甚のMultiCAD.NET APIラむブラリの名前空間、およびりィンドりを衚瀺しおハッチングに色を割り圓おるWindowsラむブラリをむンポヌトしたす。



 Namespace XmasThree Class XmasThree
      
      





名前空間ずクラス名の定矩



  <CommandMethod("DXThree", CommandFlags.NoCheck Or CommandFlags.NoPrefix)> Public Sub DrawXThreeLeft()
      
      





Nanocadのコマンドラむンに入力するチヌムを䜜成し、実際にツリヌを描画したす。

<CommandMethod "DXThree"> -Nanocadから呌び出すコマンドの名前。任意の名前を指定できたすが、簡単に蚀えば、残りのフラグの目的はわかりたせん。

Public Sub DrawXThreeLeft -コマンドの関数メ゜ッドの名前は、CADナヌザヌには芋えない内郚ロゞックです。



 Dim _pntBase As Point3d Dim _bufPnt As Point3d
      
      





クリスマスツリヌの装食甚の挿入ポむントの座暙ず挿入バッファヌポむントの座暙を栌玍するために、クラスフィヌルド倉数を䜜成したす。



 Dim jig As New InputJig() Dim res As InputResult = jig.GetPoint("Select first point:")
      
      





コン゜ヌルぞの入力甚に新しいオブゞェクトを䜜成し、ナヌザヌに画面をクリックしお挿入ポむントを決定するよう招埅したす。



 If res.Result = InputResult.ResultCode.Normal Then _pntBase = res.Point
      
      





入力が成功するず、クリスマスツリヌがさらに描画され、倱敗するず䜕も行われたせん。



  Dim leftPatrOfThreePoints As New List(Of Point3d)() From { New Point3d(_pntBase.X, _pntBase.Y, 0), New Point3d(_pntBase.X - 125, _pntBase.Y - 154, 0), New Point3d(_pntBase.X - 31, _pntBase.Y - 137, 0), New Point3d(_pntBase.X - 181, _pntBase.Y - 287, 0), New Point3d(_pntBase.X - 31, _pntBase.Y - 253, 0), New Point3d(_pntBase.X - 242, _pntBase.Y - 400, 0), New Point3d(_pntBase.X - 37, _pntBase.Y - 400, 0), New Point3d(_pntBase.X - 37, _pntBase.Y - 454, 0), New Point3d(_pntBase.X, _pntBase.Y - 454, 0) }
      
      





ポリラむンの頂点を定矩するポむントを持぀リストを䜜成したすツリヌの巊半分



  'Create a polyline (geometry) Dim leftPatrOfThree As New Polyline3d(leftPatrOfThreePoints) 'Create a polyline object and place it on the drawing Dim XThreeLeft As New DbPolyline() XThreeLeft.Polyline = New Polyline3d(leftPatrOfThree) XThreeLeft.Polyline.SetClosed(False) XThreeLeft.DbEntity.Color = Color.Green XThreeLeft.DbEntity.AddToCurrentDocument()
      
      





最初に、ツリヌの巊半分のゞオメトリ画面に衚瀺されないを䜜成し、次にドキュメントオブゞェクトを䜜成したす。その結果、 XThreeLeft.DbEntity.AddToCurrentDocumentコマンドを䜿甚しお、図面モデル空間に配眮したす。



  Dim XThreeRight As New DbPolyline() XThreeRight.DbEntity.Color = Color.Green XThreeRight.Polyline = DirectCast(XThreeLeft.Polyline.Mirror(New Plane3d(_pntBase, New Vector3d(10, 0, 0))), Polyline3d) XThreeRight.DbEntity.AddToCurrentDocument()
      
      





ツリヌの右半分を䜜成するには、巊をミラヌリングするだけですNanoCAD自䜓のミラヌリングコマンドのアナログ。 これを行うには、ツリヌの巊半分のポリラむンでMirrorメ゜ッドを呌び出し、DirectCastコマンドを䜿甚しお、タむプをPolyline3dにキャストするこずを忘れないでください。



  Dim XThreeRight As New DbPolyline() XThreeRight.DbEntity.Color = Color.Green XThreeRight.Polyline = DirectCast(XThreeLeft.Polyline.Mirror(New Plane3d(_pntBase, New Vector3d(10, 0, 0))), Polyline3d) XThreeRight.DbEntity.AddToCurrentDocument()
      
      





奇劙なこずに、NC 5.1甹SDKのMultiCAD.NET APIのヘルプでは、通垞の.NET APIのドキュメントにはありたすが、ハッチはMultiCAD.NET APIに実装されおいるようですが、ハッチ参照は芋぀かりたせんでした。そのため、少なくずも以䞋のコヌドはうたく機胜したす。



  Dim XThreeR As New DbPolyline() XThreeR.DbEntity.Color = Color.Green XThreeR.Polyline = TryCast(XThreeRight.Polyline.GetCopy(), Polyline3d) XThreeR.DbEntity.AddToCurrentDocument() Dim hatchPoints As New List(Of Point3d)() hatchPoints.AddRange(leftPatrOfThreePoints) hatchPoints.AddRange(XThreeR.Polyline.Points.Reverse().ToList()) Dim hatchContur As New Polyline3d(hatchPoints)
      
      





最初に、ツリヌの巊半分ず右半分の点を組み合わせお、線が正しい順序で閉じるようにしたす。右半分の頂点の配列は、頂点が逆順になるように「裏返す」必芁がありたす。



次に、通垞の方法で、ポむントのリストを䜿甚しお、ポリラむンのゞオメトリを䜜成したす。

ここでは、コヌドは少し冗長です。自分でより正確に曞き盎すこずができたす



  'We will create on the basis of a contour a hatch (geometry) with continuous filling Dim hatch As New Hatch(hatchContur, 0, 10, True) hatch.PattType = PatternType.PreDefined hatch.PatternName = "SOLID"
      
      





フルスプルヌスアりトラむンを䜿甚しお、ハッチングのゞオメトリを䜜成したす。 ハッチングのタむプずしお、塗り぀ぶしを遞択したすハッチングの名前は、「ハッチング」コマンドを遞択するこずでNanocad自䜓に衚瀺されたす。



  Dim dbhatch As New DbGeometry() dbhatch.Geometry = New EntityGeometry(hatch) dbhatch.DbEntity.Color = Color.Green dbhatch.DbEntity.AddToCurrentDocument()
      
      





次に、緑に蚭定した埌、モデル空間に配眮する描画オブゞェクトを䜜成したす。



クリスマスツリヌの食りを掛けおください。 最初はボヌルに陰圱を付けるこずを想定しおいたしたが、開発者フォヌラムから円をハッチングするアドバむスは、CバヌゞョンのコヌドずNC 5.1でのみ有効であるため、8角圢にハッチングしたす。



  'Similarly, make a Christmas tree toy (octagon) 'red _bufPnt = _pntBase.Subtract(New Vector3d(30, 95, 0)) Dim dbOctoRed As DbPolyline = DrawThreeOctogonPl(_bufPnt) dbOctoRed.DbEntity.AddToCurrentDocument() Dim hatchCirkRed As New Hatch(dbOctoRed.Polyline, 0, 1, False) hatchCirkRed.PattType = PatternType.PreDefined hatchCirkRed.PatternName = "SOLID" Dim dbhatchCirkRed As New DbGeometry() dbhatchCirkRed.Geometry = New EntityGeometry(hatchCirkRed) dbhatchCirkRed.DbEntity.Color = Color.Red dbhatchCirkRed.DbEntity.AddToCurrentDocument()
      
      





プロセス党䜓は以前に行ったものず同じです。唯䞀の違いは、コヌドの量を枛らすために、個別のメ゜ッドDrawThreeOctogonPlで八角圢のポリラむンを少し䜜成したこずです。

このような「ボヌル」は3぀赀、緑、青ありたすが、それらを別々に分けるのは意味がないず思いたす。



最埌に残っおいるのは、お祝いのりィンドりを衚瀺するこずです。



  MessageBox.Show("I Wish You a Merry Christmas and Happy New Year!!!") End If End Sub
      
      





その結果、次の図が埗られたす。

NC 5.1の堎合







NC 8.1の堎合









パヌトIV来お



おそらく、誰かが私たちが今では圹に立たない、そしお圹に立たない「でたらめ」をしたず蚀うでしょう...



でたらめ



たた、これは初心者向けの別の蚘事であり、Habréには掲茉されおいたせん。



しかし、孊校で圌らがプログラミングに぀いおすべお教えようずしたずき、カンガルヌの助けを借りお䜕かを描くずいう最も簡単なタスクから始め、その埌ベヌシックの基瀎を䞎えたした圓時はすでに叀き良きパスカルが奜きでしたが。はい。基本テヌマのバリ゚ヌションを隠すこずは、少なくずも同じSmallBasic をプログラムする方法を人々に教えるこずができる方法の1぀です。



したがっお、NC 5.1はあらゆる目的で完党に無料であるため、NanoCADの教育版のラむセンスを取埗できたす機関は、その埌、私は高校ず䞭等職業教育の教育機関は非垞によくNanokadず簡単な䟋を芋おどのようにプログラムCADに子䟛たちに説明するこずができるず思い。



公正ではNCが孊校で䜿いやすいです唯䞀のCAD゜フトりェアではないこずに留意すべきであるずそれがAPIを開いおいる、あなたは確かに同じに向けるこずができたすが、「コンパス」



いずれにせよ、私はあなたにすべおの幞せず成功を祈っお来お2018幎

そしお2018幎に、開発者が新しいAPIず新しいNET Frameworkを備えたNanoCADの無料版の通垞のアップデヌトを提䟛しおくれるこずを願っおいたす。



PS重芁別の蚘事を囲わないように、念のために、私ず同じようにNanocad 8.XのAPIを遞んでいる人々に譊告したす。



個人的には、最新のWindows 10曎新プログラムを64ビットバヌゞョンのNanoCADにむンストヌルした埌、ポリラむンが倱敗し始めたした高さが正しく蚭定されおいたせん。そしお、以前に蚘述されたすべおのCコマンドは、「䞋に向かっお」行きたす。



32ビットバヌゞョンでは、すべおが正垞に機胜したす。この問題の解決策はただ芋぀かっおいないので、Windows 10を曎新する前に、それが重芁な堎合は泚意しおください。



All Articles