フォローする

.NET - DrawJigを使用してオブジェクトを移動させる

更新 : 2017-02-02 07:25:51 UTC

.NET API を使用して、選択した図形をユーザー入力の値を元に移動させるときに、
DrawJigを使用することで、マウスの操作によって変更される各図形の移動結果を、
リアルタイムに更新させて確認する事が可能になります。

サンプルコードは以下の通りです。 

VB.NET

Imports GrxCAD.ApplicationServices
Imports GrxCAD.Colors
Imports GrxCAD.DatabaseServices
Imports GrxCAD.EditorInput
Imports GrxCAD.Export_Import
Imports GrxCAD.Geometry
Imports GrxCAD.GraphicsSystem
Imports GrxCAD.LayerManager
Imports GrxCAD.PlottingServices
Imports GrxCAD.Publishing
Imports GrxCAD.Runtime
Imports GrxCAD.Windows
Imports CADException = GrxCAD.Runtime.Exception

<Assembly: CommandClass(GetType(Sample_VB.SampleCmd))>

Namespace Sample_VB
    Public Class SampleCmd : Implements IExtensionApplication
        '初期化処理
        Public Sub Initialize() Implements IExtensionApplication.Initialize
            Try
                Dim oEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
                oEd.WriteMessage("アセンブリをロードしました")
            Catch ex As CADException
                Debug.Print(ex.Message + ex.StackTrace)
            End Try
        End Sub

        '解放処理
        Public Sub Terminate() Implements IExtensionApplication.Terminate
        End Sub

        'ドロージグを使用する(VB.NET)
        <CommandMethod("SampleCmd")>
        Public Sub SampleCommand()
            Dim icDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim icEd = icDoc.Editor

            '移動させるオブジェクトを選択
            Dim icSelectRes As PromptSelectionResult = icEd.GetSelection()
            If icSelectRes.Status <> PromptStatus.OK Then Return

            '移動の基点を指定
            Dim icPointRes As PromptPointResult = icEd.GetPoint(vbCrLf + "基点を指定")
            If icPointRes.Status <> PromptStatus.OK Then Return

            Using icTrans As Transaction = icDoc.TransactionManager.StartTransaction()
                Dim icMoveJig As MoveEntityJig = New MoveEntityJig(icPointRes.Value)
                For Each icSelObj As SelectedObject In icSelectRes.Value
                    icMoveJig.AddEntity(icTrans.GetObject( _
                    icSelObj.ObjectId, OpenMode.ForWrite))
                Next
                If icEd.Drag(icMoveJig).Status = PromptStatus.OK Then
                    icMoveJig.TransformEntities()
                    icTrans.Commit()
                Else
                    icTrans.Abort()
                End If
            End Using
        End Sub
    End Class

    Public Class MoveEntityJig
        Inherits DrawJig

        Private BasePoint As Point3d = Point3d.Origin
        Private TargetPoint As Point3d = Point3d.Origin
        Private EntityList As List(Of Entity)

        Public ReadOnly Property ActiveUCS() As Matrix3d
            Get
                Return Application.DocumentManager.MdiActiveDocument.Editor. _
                    CurrentUserCoordinateSystem
            End Get
        End Property
        Public ReadOnly Property Mat
            Get
                Return Matrix3d.Displacement(BasePoint.GetVectorTo(TargetPoint))
            End Get
        End Property

        Public Sub New( _
            ByVal basePt As Point3d, _
            Optional ByVal entList As List(Of Entity) = Nothing)
            BasePoint = basePt.TransformBy(ActiveUCS)
            EntityList = IIf(entList IsNot Nothing, entList, New List(Of Entity)())
        End Sub
        Public Sub AddEntity(ByVal addEnt As Entity)
            EntityList.Add(addEnt)
        End Sub
        Public Sub TransformEntities()
            For Each ent As Entity In EntityList
                ent.TransformBy(Mat)
            Next
        End Sub

        'サンプリング処理
        Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus
            Dim jigPointOpt As JigPromptPointOptions = _
                New JigPromptPointOptions(vbCrLf + "移動先を指定")
            jigPointOpt.UseBasePoint = True
            jigPointOpt.BasePoint = BasePoint
            jigPointOpt.Cursor = CursorType.RubberBand

            Dim pmtPointRes As PromptPointResult = prompts.AcquirePoint(jigPointOpt)

            If pmtPointRes.Status <> PromptStatus.OK Then Return SamplerStatus.Cancel

            If TargetPoint.DistanceTo(pmtPointRes.Value) > 0 Then
                TargetPoint = pmtPointRes.Value
                Return SamplerStatus.OK
            Else
                Return SamplerStatus.NoChange
            End If
        End Function

        '描画の更新
        Protected Overrides Function WorldDraw( _
            draw As GrxCAD.GraphicsInterface.WorldDraw) As Boolean
            If draw.Geometry IsNot Nothing Then
                For Each ent As Entity In EntityList
                    Using drawEnt As Entity = ent.Clone()
                        drawEnt.TransformBy(Mat)
                        draw.Geometry.Draw(drawEnt)
                    End Using
                Next
            End If
            Return True
        End Function
    End Class
End Namespace

 

C#

using GrxCAD.ApplicationServices;
using GrxCAD.Colors;
using GrxCAD.DatabaseServices;
using GrxCAD.EditorInput;
using GrxCAD.Export_Import;
using GrxCAD.Geometry;
using GrxCAD.GraphicsSystem;
using GrxCAD.LayerManager;
using GrxCAD.PlottingServices;
using GrxCAD.Publishing;
using GrxCAD.Runtime;
using GrxCAD.Windows;
using CADException = GrxCAD.Runtime.Exception;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

[assembly: CommandClass(typeof(Sample_CS.SampleCmd))]

namespace Sample_CS
{
    public class SampleCmd : IExtensionApplication
    {
        //初期化処理
        public void Initialize()
        {
            #region "初期化処理"
            try
            {
                DocumentCollection icDocs = Application.DocumentManager;
                Document icDoc = icDocs.MdiActiveDocument;
                Database icDb = icDoc.Database;
                Editor icEd = icDoc.Editor;
                icEd.WriteMessage("アセンブリをロードしました");
            }
            catch (CADException ex)
            {
                Debug.Print(ex.Message + ex.StackTrace);
            }
            #endregion
        }

        //解放処理
        public void Terminate()
        {
        }

        //ドロージグを使用する(C#)
        [CommandMethod("SampleCmd")]
        public void SampleCommand()
        {
            Document icDoc = Application.DocumentManager.MdiActiveDocument;
            Editor icEd = icDoc.Editor;

            //移動させるオブジェクトを選択
            PromptSelectionResult icSelRes = icEd.GetSelection();
            if (icSelRes.Status != PromptStatus.OK) return;

            //移動の基点を指定
            PromptPointResult icPointRes = icEd.GetPoint("\n基点を指定");
            if (icPointRes.Status != PromptStatus.OK) return;

            using (Transaction icTrans = icDoc.TransactionManager.StartTransaction())
            {
                MoveEntityJig icMoveJig = new MoveEntityJig(icPointRes.Value);

                foreach (SelectedObject icSelObj in icSelRes.Value)
                {
                    icMoveJig.AddEntity(icTrans.GetObject(
                        icSelObj.ObjectId,OpenMode.ForWrite) as Entity);
                }
                if (icEd.Drag(icMoveJig).Status == PromptStatus.OK)
                {
                    icMoveJig.TransformEntities();
                    icTrans.Commit();
                }
                else
                    icTrans.Abort();
            }
        }
    }

    public class MoveEntityJig : DrawJig
    {
        private Point3d BasePoint = Point3d.Origin;
        private Point3d TargetPoint = Point3d.Origin;
        private List<Entity> EntityList;

        public Matrix3d ActiveUCS
        {
            get {return Application.DocumentManager.MdiActiveDocument.
Editor.CurrentUserCoordinateSystem;} } public Matrix3d Mat { get {return Matrix3d.Displacement(BasePoint.GetVectorTo(TargetPoint));} } public MoveEntityJig(Point3d basePt, List entList = null) { BasePoint = basePt.TransformBy(ActiveUCS); EntityList = entList ?? new List(); } public void AddEntity(Entity addEnt) { EntityList.Add(addEnt); } public void TransformEntities() { EntityList.ForEach(ent => ent.TransformBy(Mat)); } //サンプリング処理 protected override SamplerStatus Sampler(JigPrompts prompts) { JigPromptPointOptions jigPointOpt =
new JigPromptPointOptions("\n移動先を指定"); jigPointOpt.UseBasePoint = true; jigPointOpt.BasePoint = BasePoint; jigPointOpt.Cursor = CursorType.RubberBand; PromptPointResult pmtPointRes = prompts.AcquirePoint(jigPointOpt); if (pmtPointRes.Status == pmtPointRes.Status != PromptStatus.OK) return SamplerStatus.Cancel; if (TargetPoint.DistanceTo(pmtPointRes.Value) >0) { TargetPoint = pmtPointRes.Value; return SamplerStatus.OK; } else return SamplerStatus.NoChange; } //描画の更新 protected override bool WorldDraw(GrxCAD.GraphicsInterface.WorldDraw draw) { if (draw.Geometry != null) { foreach (Entity ent in EntityList) { using (Entity drawEnt = (Entity)ent.Clone()) { drawEnt.TransformBy(Mat); draw.Geometry.Draw(drawEnt); } } } return true; } } }

このサンプルではMOVEコマンドの様にオブジェクトを移動させるコマンドとなっていますが、
回転や拡大縮小でも同様に、ユーザー入力中に変更結果を確認しつつ入力が行えます。

ポリラインやスプラインなどのオブジェクトを作図する時でも、入力の過程はドロージグを使用して、
ユーザーによる全ての入力を問題なく完了した後にデータベースに登録させるようにすることで、
入力のキャンセルが発生した場合には、コマンド実行前の状態にすぐ戻す様な事も行えます。

この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています
他にご質問がございましたら、リクエストを送信してください

コメント