Author Topic: Bricscad ET Chspace like command  (Read 5056 times)

0 Members and 1 Guest are viewing this topic.

Jerry J

  • Newt
  • Posts: 48
Bricscad ET Chspace like command
« on: April 10, 2010, 08:28:59 AM »
I have a need of assistance.  I am trying to program the Acad Express Tools Chang Space command for copying entities from MS to PS with little luck.  I'm using VBA because it's easy on debug/run, luckily.  I figured out the copying and rotation to viewtwist but I can't seem to get transformcoordinates to work as I expect.  I need to transform text insertion point in model to same in acadPViewport.  Anyone achieved this in Bricscad?  The reason I say Bricscad, the twist did not seem to work as I expected or as sample Acad code showed.  Resorted to direction - target.  I keep having regen problems when it switches from Mspace to Pspace that might be wreaking havoc.  Any suggestions would be appreciated.

hmspe

  • Bull Frog
  • Posts: 362
Re: Bricscad ET Chspace like command
« Reply #1 on: April 10, 2010, 12:27:30 PM »
It would be easier to use the existing express tool.  It is readily available if you search on Google for 'chspace.lsp'.  The version at allexperts.com is from Autodesk Australia and the copyright notice allows free use.  Note that this is not the same version as in the express tools in the USA versions of Autocad.  There are also copies of the version by Randy Kintzley posted, which is what is was in the express tools in the USA.
"Science is the belief in the ignorance of experts." - Richard Feynman

Jerry J

  • Newt
  • Posts: 48
Re: Bricscad ET Chspace like command
« Reply #2 on: April 10, 2010, 04:48:13 PM »
I think that will have to do, basically I can copy with base and achieve the same. Would still be interested if someone can convert MSpace coordinate to PSace coordinate using com, VBA or Vlax.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8703
  • AKA Daniel
Re: Bricscad ET Chspace like command
« Reply #3 on: April 10, 2010, 11:47:36 PM »
Hi Jerry,

My VBA bites, so maybe you can translate it. I tested this in a few ports, some of them rotated (dview) and it seemed to work.. YMMV, but I hope it gets you started.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

using BricscadApp;
using BricscadDb;
using RxNet.ApplicationServices;
using RxNet.Runtime;
using RxNet.Geometry;
using RxNet.RxGlobal;


namespace Bricscad
{
  public static class Commands
  {

    // from papaerspace vp is active

    [CommandMethod("doit")]
    public static void test01()
    {
      try
      {
        // get app stuff
        var app = Application.AcadApplication as AcadApplication;
        var doc = app.ActiveDocument;
        var mspace = doc.ModelSpace;

        GlobalFunctions.MSpace();

        //select an entity and copy
        object opnt, oent, pairs = null;
        doc.Utility.GetEntity(out oent, out opnt, "Select Entity");
        AcadEntity entity = oent as AcadEntity;
        AcadEntity[] ocol = { entity };
        object[] ncol = doc.CopyObjects((object)ocol, doc.PaperSpace, ref pairs);
        AcadEntity nentity = ncol[0] as AcadEntity;

        // get the center of both entities bounding box
        Point3d cenent = getBBCenter(entity);
        Point3d cennent = getBBCenter(nentity);

        // scale
        nentity.ScaleEntity(cennent.ToArray(), doc.ActivePViewport.CustomScale);

        // rotate
        nentity.Rotate(cennent.ToArray(), doc.ActivePViewport.TwistAngle);

        // move
        nentity.Move(cennent.ToArray(),
          doc.Utility.TranslateCoordinates(
          cenent.ToArray(), AcCoordinateSystem.acUCS, AcCoordinateSystem.acPaperSpaceDCS, 0));


        // delete
        entity.Delete();

        GlobalFunctions.PSpace();
      }
      catch (System.Exception ex)
      {
        GlobalFunctions.Printf("\n{0}\n{1}",
         ex.Message, ex.StackTrace);
      }
    }

    static Point3d getBBCenter(AcadEntity ent)
    {
      object omin, omax;
      ent.GetBoundingBox(out omin, out omax);
      Point3d min = new Point3d((double[])omin);
      Point3d max = new Point3d((double[])omax);
      return new Point3d(min[0] + ((max[0] - min[0]) / 2),
                         min[1] + ((max[1] - min[1]) / 2), 0);
    }
  }
}


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8703
  • AKA Daniel
Re: Bricscad ET Chspace like command
« Reply #4 on: April 10, 2010, 11:50:26 PM »
the highlighted items were cloned from MS to PS, the VP was twisted...
« Last Edit: April 10, 2010, 11:59:25 PM by Daniel »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8703
  • AKA Daniel
Re: Bricscad ET Chspace like command
« Reply #5 on: April 11, 2010, 12:21:34 AM »
works on a selection set, you may want to filter dims

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

using BricscadApp;
using BricscadDb;
using RxNet.ApplicationServices;
using RxNet.Runtime;
using RxNet.Geometry;
using RxNet.RxGlobal;


namespace Bricscad
{
  public static class Commands
  {

    // from papaerspace vp is active

    [CommandMethod("doit")]
    public static void test01()
    {
      AcadSelectionSet selection = null;
      try
      {
        // get app stuff
        var app = Application.AcadApplication as AcadApplication;
        var doc = app.ActiveDocument;

        GlobalFunctions.MSpace();

        selection = doc.SelectionSets.Add("MySel");
        selection.SelectOnScreen();
        foreach (AcadObject o in selection)
        {
          AcadEntity e = o as AcadEntity;
          if (e != null)
            cloneToPspace(e, doc);
        }

        foreach (AcadObject o in selection)
        {
          AcadEntity e = o as AcadEntity;
          if (e != null)
            e.Delete();
        }

        GlobalFunctions.PSpace();
      }
      catch (System.Exception ex)
      {
        GlobalFunctions.Printf("\n{0}\n{1}",
         ex.Message, ex.StackTrace);
      }
      finally
      {
        selection.Delete();
      }
    }

    static void cloneToPspace(AcadEntity entity, AcadDocument doc)
    {
      try
      {
        object pairs = null;
        AcadEntity[] ocol = { entity };
        object[] ncol = doc.CopyObjects((object)ocol, doc.PaperSpace, ref pairs);
        AcadEntity nentity = ncol[0] as AcadEntity;

        // get the center of both entities bounding box
        Point3d cenent = getBBCenter(entity);
        Point3d cennent = getBBCenter(nentity);

        // scale
        nentity.ScaleEntity(cennent.ToArray(), doc.ActivePViewport.CustomScale);

        // rotate
        nentity.Rotate(cennent.ToArray(), doc.ActivePViewport.TwistAngle);

        // move
        nentity.Move(cennent.ToArray(),
          doc.Utility.TranslateCoordinates(
          cenent.ToArray(), AcCoordinateSystem.acUCS, AcCoordinateSystem.acPaperSpaceDCS, 0));
      }
      catch {/*hehe*/}
    }

    static Point3d getBBCenter(AcadEntity ent)
    {
      object omin, omax;
      ent.GetBoundingBox(out omin, out omax);
      Point3d min = new Point3d((double[])omin);
      Point3d max = new Point3d((double[])omax);
      return new Point3d(min[0] + ((max[0] - min[0]) / 2),
                         min[1] + ((max[1] - min[1]) / 2), 0);
    }
  }
}


Jerry J

  • Newt
  • Posts: 48
Re: Bricscad ET Chspace like command
« Reply #6 on: April 11, 2010, 05:46:16 AM »
Boy you made the full blown command.  Looks good. Haven't tried your code because I was kind of in the middle of a specific use.  I was given a survey map with all the annotations (bearings, distances, notes ..) which need to be divided into 3 sheets with two different scales and overlapping areas. Some of the text needed to remain horizontal, some aligned, some 1:50 some 1:100. Anyway here is the code in VBA I crunched on;

Code: [Select]
Sub msTxt2ps()
      Dim pp As Variant
      Dim pt1 As Variant
      Dim ent As AcadEntity
      Dim aTxt As AcadText
      Dim mTxt As AcadMText
      Dim nTxt As AcadText
      Dim amTxt As AcadMText
      Dim mAtt As Variant
     
      Dim ss As AcadSelectionSet
      Dim Code(0) As Integer
      Dim Val(0) As Variant
      Set ss = ThisDrawing.ActiveSelectionSet
      ss.Clear
          Code(0) = 0
          Val(0) = "TEXT,MTEXT"
         
     Dim styl As AcadTextStyle
     Set styl = ThisDrawing.ActiveTextStyle
     
      Dim ps As AcadPViewport
      Set ps = ThisDrawing.ActivePViewport
      pss = ps.CustomScale
      psrot = ps.TwistAngle
     
      ss.SelectOnScreen Code, Val
      ThisDrawing.ActiveSpace = acPaperSpace
For Each ent In ss
      If TypeOf ent Is AcadText Then
          Set aTxt = ent
          pt1 = aTxt.InsertionPoint
          pt1 = transPt(pt1)
          rot = Math.Round(aTxt.Rotation, 5)
          pt2 = aTxt.TextAlignmentPoint
          pt2 = transPt(pt2)
          txtht = aTxt.Height * pss
          Set nTxt = ThisDrawing.PaperSpace.AddText(aTxt.TextString, pt1, txtht)
          nTxt.Alignment = aTxt.Alignment
          nTxt.TextAlignmentPoint = pt2
          nTxt.InsertionPoint = pt1
               If rot = 0# Then
               nTxt.Rotation = 0#
               Else
               nTxt.Rotation = rot + psrot
               End If
          Else
          Set mTxt = ent
          pt1 = mTxt.InsertionPoint
          pt1 = transPt(pt1)
          rot = Math.Round(mTxt.Rotation, 5)
          wid = mTxt.Width
          mAtt = mTxt.AttachmentPoint
          Set amTxt = ThisDrawing.PaperSpace.AddMText(pt1, wid, mTxt.TextString)
          amTxt.AttachmentPoint = mAtt
               If rot = 0# Then
               amTxt.Rotation = 0#
               Else
               amTxt.Rotation = rot + psrot
               End If
          amTxt.StyleName = styl.Name
          amTxt.ScaleEntity pt1, pss
     End If
Next
     
End Sub

Function transPt(ByRef pt1 As Variant) As Variant
     pt1 = ThisDrawing.Utility.TranslateCoordinates(pt1, acUCS, acDisplayDCS, 0)
     pt1 = ThisDrawing.Utility.TranslateCoordinates(pt1, acDisplayDCS, acPaperSpaceDCS, 0)
     transPt = pt1
End Function

Funny that you don't have to apply the CustomScale factor to Mtext.Width. Or so it seemed to me.
I'll be reviewing your code so that maybe I can C# some Mspace2Pspace tools for the future.  Thanks a bunch.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8703
  • AKA Daniel
Re: Bricscad ET Chspace like command
« Reply #7 on: April 11, 2010, 07:23:57 AM »
Nice work! I see you're creating new entities whereas I am using the CopyObjects function...why I needed to scale the objects.
BTY, I only used C# because I'm more comfortable with it. there is no reason why this couldn't be ported to VBA... its all COM  :-)

Jerry J

  • Newt
  • Posts: 48
Re: Bricscad ET Chspace like command
« Reply #8 on: April 12, 2010, 05:19:28 AM »
I was creating new objects because I figured easiest way to get them on a new layer (current layer) and I didn't want to delete originals just turn them off since the same text may need to be copied to different layouts.  My next project is to get the monument symbols (blocks) into PS for the same reasons.  Would be simple except the symbols contain wipeout entities.  Copying them doesn't seem to keep the draworder.  Bringing them to front once they are in paper space seems to have mixed success. I must be overlooking something.  Anyway, got to finish this mapping asap so will have to wait for coding a routine.