Author Topic: Mid point of a blockref  (Read 10021 times)

0 Members and 1 Guest are viewing this topic.

Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #15 on: April 25, 2008, 11:47:57 AM »
try this

Code: [Select]
Option Explicit
Public Sub blkCent()
  Dim minExt As Variant
  Dim maxExt As Variant
  Dim pntCent(0 To 2) As Double
   
  'lineObj.GetBoundingBox minExt, maxExt

  Dim strSet As String
  Dim strSetName As String
  Dim intGroup(0) As Integer
  Dim varGroup(0) As Variant
  Dim objSelSet As AcadSelectionSet
  Dim objSelSets As AcadSelectionSets
  Dim strBlkName As String
  Dim objBlkRef As AcadBlockReference
  Dim objEnt As AcadEntity
  Dim intCnt As Integer
 
 
  Set objSelSets = ThisDrawing.SelectionSets
  strSetName = 1
  intGroup(0) = 0
  varGroup(0) = "insert"
  Set objSelSet = objSelSets.Add(strSetName)
  'objSelSet.Select acSelectionSetAll, , , intGroup, varGroup
  objSelSet.SelectOnScreen intGroup, varGroup
  For Each objEnt In objSelSet
   If TypeOf objEnt Is AcadBlockReference Then
   ' If objEnt.ObjectName = "AcDbBlockreference" Then
      Set objBlkRef = objEnt
      objBlkRef.GetBoundingBox minExt, maxExt
      pntCent(0) = (minExt(0) + maxExt(0)) / 2
      pntCent(1) = (minExt(1) + maxExt(1)) / 2
      pntCent(2) = (minExt(2) + maxExt(2)) / 2
    End If
     Debug.Print pntCent(0); pntCent(1)
  Next objEnt
End Sub

ML

  • Guest
Re: Mid point of a blockref
« Reply #16 on: April 25, 2008, 02:36:19 PM »

Bob

That did not quite get it, then I looked at the acad example as well
I started to screw with it a bit then thought I best just finish "this time" manually
I had to do it with 77 blocks

OK, so I finished that task, next time, I need to have an automated way of getting it.

If I get a few minutes this afternoon, I will look at it more.

Bob, would the bound box be similar to drawing a rectangle around the whole blockref?

If so, that is precisely what I need.

I see that you did the required math to capture the midpoint, I will need to expound; I think it would be good if when I select the blockref, that midpint will become my picked point, then I can move the blockref right into place.

Think it can be done?

Any help is certainly (as always) appreciated

Thanks!

Mark

Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #17 on: April 25, 2008, 03:06:15 PM »

Bob

That did not quite get it, then I looked at the acad example as well
I started to screw with it a bit then thought I best just finish "this time" manually
I had to do it with 77 blocks
hmm.  ok.

OK, so I finished that task, next time, I need to have an automated way of getting it.

If I get a few minutes this afternoon, I will look at it more.
OK, shout if you get stumped.
Bob, would the bound box be similar to drawing a rectangle around the whole blockref?

If so, that is precisely what I need.
yep, precisely.
I see that you did the required math to capture the midpoint, I will need to expound; I think it would be good if when I select the blockref, that midpint will become my picked point, then I can move the blockref right into place.

Think it can be done?
yep.  You can, after selection, prompt for a "move to point," then move it programatically.

ML

  • Guest
Re: Mid point of a blockref
« Reply #18 on: April 25, 2008, 05:01:35 PM »

Thanks Bob!

I will take a look at it.

Unfortunately, we are under the gun to get this done by Monday.

I will definitively look at it next week. If I get jammed up, may be you could lend me a hand.

Thanks Bob

Have a good weekend

M



Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #19 on: April 25, 2008, 07:55:23 PM »
I only tried it once, and the block was fairly simple, but this worked for me.
Code: [Select]
Option Explicit
Public Sub blkCent()
  Dim minExt As Variant
  Dim maxExt As Variant
  Dim pntCent(0 To 2) As Double
  Dim strSet As String
  Dim strSetName As String
  Dim intGroup(0) As Integer
  Dim varGroup(0) As Variant
  Dim objSelSet As AcadSelectionSet
  Dim objSelSets As AcadSelectionSets
  Dim objBlkRef As AcadBlockReference
  Dim objEnt As AcadEntity
  Dim pntMoveTo As Variant
 
  Set objSelSets = ThisDrawing.SelectionSets
  strSetName = 1
  intGroup(0) = 0
  varGroup(0) = "insert"
  KillSet strSetName
  Set objSelSet = objSelSets.Add(strSetName)
  objSelSet.SelectOnScreen intGroup, varGroup
  For Each objEnt In objSelSet
   If TypeOf objEnt Is AcadBlockReference Then
      Set objBlkRef = objEnt
      objBlkRef.GetBoundingBox minExt, maxExt
      pntCent(0) = (minExt(0) + maxExt(0)) / 2
      pntCent(1) = (minExt(1) + maxExt(1)) / 2
      pntCent(2) = (minExt(2) + maxExt(2)) / 2
    objBlkRef.Highlight True
    pntMoveTo = ThisDrawing.Utility.GetPoint(, "Select Destination Point: ")
    objBlkRef.Move pntCent, pntMoveTo
    End If
  Next objEnt
End Sub

Function KillSet(strSet As String)
  Dim objSelSet As AcadSelectionSet
  Dim objSelSets As AcadSelectionSets
 
  Set objSelSets = ThisDrawing.SelectionSets
     
  For Each objSelSet In objSelSets
    If objSelSet.Name = strSet Then
      ThisDrawing.SelectionSets.Item(strSet).Delete
    Exit For
    End If
  Next

End Function

ML

  • Guest
Re: Mid point of a blockref
« Reply #20 on: April 25, 2008, 08:03:49 PM »

Hey Bob
Are you still in work also?
Man! No rest for the weary...shewwww

Hey, I just tried the code, it seems to be working perfectly
Good job!

I really appreciate it

I will "definitely" look at it closer on Monday

Mark

ML

  • Guest
Re: Mid point of a blockref
« Reply #21 on: April 25, 2008, 08:12:30 PM »

BOB

That kicks Ass!

Man I could have used that 7 hours ago

It will certainly come in handy for the next batch that I do

I never want to see a rectangle again :)

Thanks again!!!

Mark

Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #22 on: April 25, 2008, 08:56:04 PM »
Glad you like it.  I would still throw in some error handling if I was you.  I stripped out what was in the one I started with though don't ask me why.  I had a couple of minutes before class started this evening and was able to knock it out.  It actually wouldn't hurt to rewrite the whole thing if you get the time.  Having come from a larger, screwier routine, it could be more clean.

ML

  • Guest
Re: Mid point of a blockref
« Reply #23 on: April 25, 2008, 09:13:58 PM »

Yes, it works very well, thanks Bob!
I tried it on like 4 examples and it was exactly what I needed
I did not get any errors but if I encounter any, I will certainly address them

I will definitely look at it closer next week.
I will clean it or shorten up a bit but the method is dead on.

Before class huh?

Like I said, no rest for the weary :)

The next thing I am going to do is have ACAD write out the script files for me, then I think I will be in great shape for creating slides.

Not sure that I told you that is what I was doing?

I made a template with 75 viewports in Model, with center ticks.
1 viewport per slide.
Defined the viewport boundaries for each one.

Then wrote a script that basically repeats 75 times
view
r
5
mslide
Slidename

Then from Dos, using Slidelib, I created the slide library

We are creating image tiles for all of our symbol libraries

A lot of work upfront but worth it in the end.

Thanks again Bob

I appreciate all of the help!

M




Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #24 on: April 25, 2008, 09:28:58 PM »
I've got a vba routine at the office that writes scripts, or purports to at any rate, I don't think I ever tried it.  My counterpart in one of our other offices turned it up a while back.  I always do mine manually. 
  • I go to a command prompt
  • dir *.dwg/b>whatever.scr
  • open it in notepad
  • paste
  • down arrow
  • paste
  • down arrow
  • lather
  • rinse
  • repeat
Not the most efficient way, but it works and it's one of those old dog/new trick things.  PM me monday morning and I'll dig it up, if you want it.

Side Note:How many people here have ever written a text file with copy con>filename.txt then editted it with edlin?

Them was the days.  Not necessarily the good days, but the days.

ML

  • Guest
Re: Mid point of a blockref
« Reply #25 on: April 25, 2008, 09:58:05 PM »

Hey Bob

Yes, I would like to see that code

Bob, are you kidding? DOS is still alive and well as far as I am concerned.

I write out from DOS all of the time, to get a printout of directories and files

It is priceless

A matter of fact, to do slidelib, you must use DOS, I believe

Ever use it?

As far as scripts, that is a cool way to get all your drawings into an scr file but what about this stuff:
view
r
5
mslide
Slidename

I guess I would just output my blocknames to an scr file from DOS, then paste the rest down like you said.
Still, I think I can do something here.....

If I show you the template and you are interested, we could probably grab each viewport
Pseudo code:
Thisdrawing.Sendcommand "view" & VbCr & "r" & viewport.name & "mslide" & VbCr & blkref.name & VbCr

So, for each viewport, we could just write it out to the debugger window or better yet, I could write it out to an external .scr file from VBA....

OMG...is my brain still functioning? I can't believe it.

Hey Bob

This is not  the most effective tool but  at one point I was trying to expound on it, just got stuck.

Code: [Select]
Dim FSO,WshNetwork,WshShell,svDialog
 Dim objFile,intReturn
 Dim txtsv 'String Variables

 Set FSO = CreateObject("Scripting.FileSystemObject")
 Set WshNetwork = CreateObject("WScript.Network")
 Set WshShell = CreateObject ("WScript.shell")
 Set svDialog = CreateObject("SAFRCFileDlg.FileSave")

'Open File Save Dialog Box
  svDialog.FileName = "Temp.txt" 
  svDialog.FileType = "Text Files (*.txt)"
 
  intReturn = svDialog.OpenFileSaveDlg
  txtsv = svDialog.FileName

'Create, save and close file
 If intReturn Then
  Set objFile = FSO.CreateTextFile(txtsv)
  objFile.Close
 Else
  Wscript.Quit
 End If
 
 'Output Current Directory:

 '/k is a cmd.exe switch, it means keep the command prompt window open and wait for the next instruction.
 '/c closes the command prompt window before you see it

 WshShell.Run "cmd /C CD & dir *.dwg > dwg.txt /s /b &"
 
 
 Set WshShell = Nothing

I think you know vbs?

Just copy that code into a txt file, saveas a .vbs file and double click

It needs work but do you think I use DOS?  LOL

OK, I'll message you on Monday

Mark

ML

  • Guest
Re: Mid point of a blockref
« Reply #26 on: April 25, 2008, 10:01:40 PM »

BOB,
If you want to try to use that vbs file, don't rename the file, just browse to the directory that you want to write from/to and the temp file will get overwritten with a file from DOS called temp.
As I said, it needs work but it gets the job done.

After the data is written out, you can then rename the file to whatever

Mark

Bob Wahr

  • Guest
Re: Mid point of a blockref
« Reply #27 on: April 25, 2008, 10:31:28 PM »
re: slidelib, yes I use it
re: vbscript - I've done a bit of stuff but not a lot
re: the template, send it to me.  Not sure if I'll have any time to mess with it next week but I might.
 I don't see a way to make a slide via vba other than sendcommand and mslide which is unfortunate.


ML

  • Guest
Re: Mid point of a blockref
« Reply #28 on: April 26, 2008, 02:18:22 PM »

Yes, that is a share that you can not do mslides from VBA, via the API but at least we can do something with the sendcommand
I think acad considers image tiles legacy when in fact, I think they are still a great asset

Once the symbol library is done, if you want to add a slide, you just mslide the new block out, add the slide name to the .txt file and run slidelib again from DOS to re create the library.

I think you probably assumed by now that the code that you wrote for me was to center each blockref into the respective vport.

With that code, you can insert all of your blocks into the template I have, then quickly center each block into its respective vport.

The very first vport was created by doing a zoom extents, then drawing the viewport.
After that, I just arrayed the vport across and down.

After you freeze the viewports, you have to go into each one and select update layers, then acad will remember that the frozen state is the current layer state. This way when you make the slides via a script or however, the vports will be frozen in each slide.
The only time you would really need to do an update layers on a vport again is if you add one ( I think) but definitely if you have to re scale the vports for a new batch of blocks. I had to do that once and an update layers was necessary.

Hmmmm.there may be a way in VBA to update all vports at once; didn't think of that.

vb-scripting:
As far as vb-scripting, I love it. It has so many great uses.
I think the cool thing is that you can incorporate it into VBA as well as using it stand alone in a .vbs file.
In which case only a double click is necessary.

Off topic a bit:
Myself and CM have had a few debates with vbs.
I know that VBA has a way to write out to .txt files but I love the The textstream method in The File SystemObject of vbs.
I would not say that one way is better then the other but I will say that the textstream method with FSO is certainly a lot more flexible as I can employ it from outside of AutoCAD. Anyone that has seen some of my code has seen that I like to mix it up.
Not to mention that, if I do the vbs code in ACAD-VBA, I can copy the code snippets to vbs and do some things.
Again, that is all a personal preference.

Hey Bob,
I found a great "free" program on line; it is called vbsedit
If you want to buy it, I think it is like $47 but you can also just keep hitting evaluate if you'd like.

www.vbsedit.com

It has a built in debugger, tons of examples that you can use immediately plus it uses intellisense like VBA which is outstanding.
It also allows you to put your own code snippets in to be saved for later use.
There is also an output window that works just like locals does in VBA.
For anyone that wants to learn VB-Scripting quickly, this program is great.
To get around the evaluate message, I use the program for code examples, getting methods and debugging, then I switch over to another editor for easy tweaks, that don't require me to run the code from vbsedit.
It is certainly worth the $47, I may even go ahead and buy it.
Anyhow, I would definitely strongly suggest any serious programmer to at least check it out
I am not sure about Visual Studio because I do not own it but these features for Vb-scripting be built into there as well.

Mark






ML

  • Guest
Re: Mid point of a blockref
« Reply #29 on: April 26, 2008, 02:19:17 PM »

If you go to vbsedit, there is a flash tutorial; I was just watching it again
Man! Hats off to whomever developed that program; it is brilliant

M