CadImport.NET cad-based zooming

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
develop_sc
Posts: 2
Joined: 16 Sep 2008, 19:35

CadImport.NET cad-based zooming

Post by develop_sc » 17 Sep 2008, 17:17

Hello,
Can anybody help me with zooming to rectangle, which is given in CAD coordinates (based on editor demo)?
I have examined your example based on viewer demo, but there is an unkown variable "this.curClRect".
And related question is how to convert point in CAD coordinates to use it for positioning cad image?

Drawing large cad files is rather slow (drawn the same way as in your demo applications), can anything be done to improve drawing speed?
It seems, that cadPictBox events like paint, mouse move, mouse down etc. are invoked more than once too...

Thank you for any ideas.

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: CadImport.NET cad-based zooming

Post by support » 18 Sep 2008, 16:54

Hello!
Can anybody help me with zooming to rectangle, which is given in CAD coordinates (based on editor demo)?
I have examined your example based on viewer demo, but there is an unkown variable "this.curClRect".
And related question is how to convert point in CAD coordinates to use it for positioning cad image?
Do you use CAD Import .NET v.6.1? CAD Import .NET v.6.3 is available at: http://www.cadsofttools.com/download/cadimportnet.zip
Drawing large cad files is rather slow (drawn the same way as in your demo applications), can anything be done to improve drawing speed?
It seems, that cadPictBox events like paint, mouse move, mouse down etc. are invoked more than once too...
It is necessary to embed new drawing logic and new drawing means to improve drawing speed. This is not a quickly solved task. We have this question in our list of investigations.

Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

develop_sc
Posts: 2
Joined: 16 Sep 2008, 19:35

Re: CadImport.NET cad-based zooming

Post by develop_sc » 18 Sep 2008, 18:07

Hello Sergey,

I think I am using CadImport.NET 6.3 already (CADImport.dll 6.3.2942.31648)...
Can you help me anyway to solve this CAD point transformation?

Petr.

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: CadImport.NET cad-based zooming

Post by support » 19 Sep 2008, 15:12

Hello!

Here goes code example how to zoom on entities:

Code: Select all

    private float SetNewScale(Point topLeft, Point bottomRight)
    {
       Rectangle entRect = new Rectangle(topLeft.X - 2, topLeft.Y - 2, (bottomRight.X - topLeft.X) + 4, (bottomRight.Y - topLeft.Y) + 4);               
       float kf1 = (this.curClRect.Width * scale) / entRect.Width;
       float kf2 = (this.curClRect.Height * scale) / entRect.Height;     
       if(kf1 > kf2)
          kf1 = kf2;                                   
       return kf1;
    }

    private int cnt_ent = 0;

    private void btnShowRect_Click(object sender, System.EventArgs e)
    {     
       this.cadImage.UseDoubleBuffering = false;
       cadPictBox.Invalidate();         

       if(cnt_ent >= this.cadImage.Converter.Entities.Count)
          cnt_ent = 0;
       CADEntity ent = this.cadImage.Converter.Entities[cnt_ent];
       Point topLeft = this.cadImage.GetPoint(ent.Box.TopLeft);               
       Point bottomRight = this.cadImage.GetPoint(ent.Box.BottomRight);         
       //shift----------               
       float centerX = this.curClRect.Width / 2.0f - (bottomRight.X - topLeft.X) / 2.0f;
       float centerY = this.curClRect.Height / 2.0f - (bottomRight.Y - topLeft.Y) / 2.0f;                                                                 
       this.pos.X = this.pos.X - topLeft.X + centerX;         
       this.pos.Y = this.pos.Y - topLeft.Y + centerY;                                       
       //--------------     
       old_Pos = new PointF(cadPictBox.ClientRectangle.Width / 2, cadPictBox.ClientRectangle.Height / 2);
       this.scale = SetNewScale(topLeft, bottomRight);               
       cadPictBox.Invalidate();               
       cnt_ent++;                             
    }
Is this what you need?

BTW: variable "this.curClRect" was used in the demo applications of CAD Import .NET v.6.1. It was replaced by "this.VisibleAreaSize" in CAD Import .NET v.6.3. This variable is being counted once when loading a file and it takes part in some culculations.

Sergey.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

creepin
Posts: 19
Joined: 20 Nov 2010, 00:27

Re: CadImport.NET cad-based zooming

Post by creepin » 08 Mar 2011, 13:26

Hello,

I tried the code u gave above and also had a look at the other topics about zooming to an entity.
Now the problem is, it needs 2 to 4 clicks on a button to get the entity zoomed.

Here is my code:

Code: Select all

   Private Function SetNewScale(ByVal topleft As Point, ByVal bottomright As Point) As Single
        Dim entRect As New Rectangle(topleft.X - 2, topleft.Y - 2, (bottomright.X - topleft.X) + 4, (bottomright.Y - topleft.Y) + 4)

        Dim kf1 As Single = (Me.VisibleAreaSize.Width * Me.ImageScale / entRect.Width)
        Dim kf2 As Single = (Me.VisibleAreaSize.Height * Me.ImageScale / entRect.Height)
        If kf1 > kf2 Then
            kf1 = kf2
        End If
        Return kf1

    End Function

    Private Sub ZoomToEntity(ByVal handle As ULong)
        
        Dim ent As CADEntity = Me.cadImageFld.Converter.Entities(handle)
        Me.cadImageFld.UseDoubleBuffering = False
        cadPictBox.Invalidate()
        Dim topleft As Point = Me.cadImageFld.GetPoint(ent.Box.TopLeft)
        Dim bottomright As Point = Me.cadImageFld.GetPoint(ent.Box.BottomRight)
        Dim centerx As Single = Me.VisibleAreaSize.Width / 2 - (bottomright.X - topleft.X) / 2
        Dim centery As Single = Me.VisibleAreaSize.Height / 2 - (bottomright.Y - topleft.Y) / 2
        Dim center As New FPoint(centerx, centery, 0)

       
        Me.Position = New PointF(Me.Position.X - topleft.X + centerx, Me.Position.Y - topleft.Y + centery)
        
        Me.PreviousPosition = New PointF(cadPictBox.ClientRectangle.Width / 2, cadPictBox.ClientRectangle.Height / 2)
        Me.ImageScale = SetNewScale(topleft, bottomright)
        Me.ImagePreviousScale = Me.ImageScale
        cadPictBox.Invalidate()


    End Sub

    Private Sub Button37_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button37.Click
        Dim handle As ULong = TextBox2.Text
        ZoomToEntity(handle)
    End Sub
What have I done wrong?

Thanks in Advance!

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: CadImport.NET cad-based zooming

Post by support » 22 Mar 2011, 18:21

Hello.
The code was originally for using with the CAD Import .NET 6. So, there are few changes:

Code: Select all

        Me.cadImageFld.UseDoubleBuffering = False
        Me.cadPictBox.Invalidate()

        Dim ent As CADEntity = Me.cadImageFld.Converter.Entities(0)

        Dim topleft As Point = Me.cadImageFld.GetPoint(ent.Box.TopLeft)
        Dim bottomright As Point = Me.cadImageFld.GetPoint(ent.Box.BottomRight)
        Dim centerx As Single = Me.cadPictBox.DisplayRectangle.Width / 2 - (bottomright.X - topleft.X) / 2
        Dim centery As Single = Me.cadPictBox.DisplayRectangle.Height / 2 - (bottomright.Y - topleft.Y) / 2
        
        Me.Position = New PointF(Me.Position.X - topleft.X + centerx, Me.Position.Y - topleft.Y + centery)
        Me.PreviousPosition = New PointF(cadPictBox.DisplayRectangle.Width / 2, cadPictBox.DisplayRectangle.Height / 2)

        Dim entRect As New Rectangle(topleft.X, topleft.Y, (bottomright.X - topleft.X), (bottomright.Y - topleft.Y))

        Dim kf1 As Single = (Me.VisibleAreaSize.Width * Me.ImageScale / entRect.Width)
        Dim kf2 As Single = (Me.VisibleAreaSize.Height * Me.ImageScale / entRect.Height)
        If kf1 > kf2 Then
            kf1 = kf2
        End If
 
        Me.ImageScale = kf1
        Me.cadPictBox.Invalidate()
This snippet will work with one click. However additional conditions required to successfully implement it to demo (Viewer) project:
1. CADImage must be drawn without using doublebuffering. For example Me.useDoubleBuffering = False must be set in InitParam method of Viewer demo to meet this condition.
2. There is a modification in Viewer demo C# project that doesn't included in VB.NET project currently. There is private float temp_scale field that must be implemented in VB.NET project just in the same way as it used in C# project.

Alexander.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

gloehr006
Posts: 1
Joined: 16 Jun 2011, 01:58

Re: CadImport.NET cad-based zooming

Post by gloehr006 » 16 Jun 2011, 03:17

This code works well for me to zoom in on my entity. However I do still have a related issue.
In my case I need to do the following automatically:

1-launch viewer
2-load drawing
3-add entity
4-zoom on entity

I can get steps 1 through 3 to work fine automatically. To do this I had to remove the thread to load the file, and just load the file synchronously (this is ok for my needs).

However I cannot get the step 4 (zoom) to work automatically.
It does work fine when I fire the code from behind a button once everything is loaded. Same exact code.
I tried the window shown event which is the last event to fire in winforms as I understand it (I work more in WPF these days)

Any help is appreciated, thanks.
Greg

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: CadImport.NET cad-based zooming

Post by support » 21 Jun 2011, 17:09

Hello Greg.

The problem because CADImage object must be drawn before zooming to an entity. For example, replace the control initial invalidation from previous snippet with the following:

Code: Select all

        Dim cntrl As Control
        cntrl = New Control
        Me.cadImageFld.UseDoubleBuffering = False
        Me.cadImageFld.Draw(cntrl.CreateGraphics(), Me.cadPictBox.DisplayRectangle)
Alexander.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply