Jump to content

Using one block to make multiple unique objects


Recommended Posts

Posted

Suppose you have a block that contains one circle and two lines. Imagine an analog clock pointed at 12 O'clock.

 

What I want to do is write some code that will ask the user what time it is. Then with that information call the block from memory, rotate the lines (the hour and minute hands if you will), and allow the user to paste this anywhere in the drawing. Furthermore, the user can keep on running the program to add more clocks with different times.

 

I need help with this. I don't know where to start. (I'm using the clock just as an example, my ultimate intention is more useful, but I think this is the easiest way for me to learn what I need to learn.)

 

Thanks,

tiger1337

  • Replies 31
  • Created
  • Last Reply

Top Posters In This Topic

  • tiger1337

    15

  • dbroada

    10

  • reccakeys

    5

  • 10west

    2

Posted (edited)

Thats it, start with something easy!

 

You will have to use a dynamic block if you want multiple instances of the same block but with graphical diferences. The only other way know is to create a different name for each block as you modify it.

 

As far as writing the code break down to stages everything you want to achieve. As a start I have included a sample block (you will need to shorten the hour hand or change the colours etc.) that you can insert. If you look at it using the properties palette you can see there are 2 rotation values. These can be changed in the properties palette or by code.

 

I would start by taking an input value and change it in to a rotation.

 

ClockFace.dwg

Edited by dbroada
this time with attachment
Posted

Now I know. That is why I am not learning because I always prefer doing the hard thing...... I will try doing things like you bro, like starting with something easy. I never though of that,, what a pity.....

Posted (edited)

my "start with something easy" was meant to be ironic. It isn't too dificult if you understand dynamic blocks but I wouldn't want to tackle his problem without one.

 

AutoCAD has its own ways to make simple things difficult so don't give it any assistance!

Edited by dbroada
Posted

I am newbie here bro. I really wanted to learn things in a fast way. But I guess that's wrong. Your post enlighten me. By the way bro, I am really new to autocad. 100% empty. Mind If I ask what is Block that you both are discussing?

Posted

a block is a collection of elements that can be used as one component. you won't be able to use the block I posted above as 2004 didn't have dynamic properties but you can imagine a clock being used many times. You wouldn't want to draw it every time and you would expect the whole clock to move if you moved just one element. Try INSERT to get an idea.

Posted

Thanks Bro,

Now I know... I have plenty to ask you Bro. Hope it is ok for you. Mind if I ask you again and again.? Please?

Posted

Probably best to start a new thread in the begineers section. That way more people can contribute. Don't worry about what you ask, we all started somewhere and there is always somebody around willing to help newbies.

 

Oh, and make sure you give your thread a sensible title. I won't answer "help me" titled threads but might help a "I'm trying to ....."

Posted

Thanks Dave...

Your reply are highly appreciated. thank you so much for your time......

Posted

Great idea! I actually have some time to spare, so I'm going to work on this for the next couple of hours. I'll use your drawing, so anyone else can follow along. I'll post my progress.

 

~tiger1337

Posted

The following code assumes that ClockFace.dwg block is in the same folder as the current drawing:

 

 
Option Explicit
Public Sub InsertClock()
   Dim objBRef As AcadBlockReference
   Dim varPoint As Variant
   Dim intMin As Integer
   Dim intHour As Integer
   Dim BProps As Variant
   Dim tmpProp As AcadDynamicBlockReferenceProperty
   Dim intI As Integer

   'ask user for where to place the clock, the prompt goes
   '   through the command window
   varPoint = ThisDrawing.Utility.GetPoint(, "click where you want the clock")

   'ask for time
   intHour = ThisDrawing.Utility.GetInteger(vbCrLf & "Hour: ")
   intMin = ThisDrawing.Utility.GetInteger(vbCrLf & "Minute: ")

   'insert block (the ones and zeros are for scale and rotation)
   Set objBRef = ThisDrawing.ModelSpace.InsertBlock(varPoint, "ClockFace.dwg", 1, 1, 1, 0)

   'get properties of the block
   BProps = objBRef.GetDynamicBlockProperties

   'go find minute and hour properties and change them
   'the minute rotation property is called 'Angle'
   'the hour rotation property is called 'Angle1'

   For intI = LBound(BProps) To UBound(BProps)

       'grab one property

       Set tmpProp = BProps(intI)

       'check for minute hand
       If tmpProp.PropertyName = "Angle" Then
           tmpProp.Value = Math.Atn(1) * 4 - (CDbl(intMin) / 60) * 2 * Math.Atn(1) * 4 - 2 * Math.Atn(1)
       End If

       'check for hour hand
       If tmpProp.PropertyName = "Angle1" Then
           tmpProp.Value = Math.Atn(1) * 4 - (CDbl(intHour) / 12) * 2 * Math.Atn(1) * 4 - 2 * Math.Atn(1)
       End If

   Next intI

   'update block
   objBRef.Update

End Sub

 

Ok, now there is something I would like to change to this code, I just don't know the syntax. I would like it when the user picks a point to insert the clock that a dashed line version of the clock follows along the cursor. You know what I mean? It makes it professional and makes it easier when you need to invision where the clock needs to go.

 

Thanks,

~tiger1337

Posted

it would help if I could spell "Success" right :lol:

Posted

Very nice. It would have taken me a lot longer to get there!

Ok, now there is something I would like to change to this code, I just don't know the syntax. I would like it when the user picks a point to insert the clock that a dashed line version of the clock follows along the cursor. You know what I mean? It makes it professional and makes it easier when you need to invision where the clock needs to go.

 

Thanks,

~tiger1337

that is a problem, and one which I have never solved. There is a discussion going on in http://www.cadtutor.net/forum/showthread.php?50613-repeat-insert-block-while-loop about this while using LISP & .NET although I haven't actually read what has been said.
Posted

haha thanks dave!

 

This is the first time I am working with dynamic blocks. Could you give me a quick run through of how you added those angle paramaters to the two lines? I'm trying to replicate the same thing on another block I already created. However, I am screwing something up with actually applying the angle parameter.

 

Thanks in advance,

tiger1337

Posted

I don't use rotations very often so it is very hit & miss for me too. In this example you need one parameter and one action for each hand. I find the combined ones difficult to use so do one parameter|action at a time. I just added the parameter with the centre of the circle as the base point and chose the 12 o'clock position (I think) as its location. I then went into properties and made it have 0 grips. Invoke the rotate action, select the rotate parameter and select the vertical line and I think that was it. Repeat in a similar manner for the hour hand.

Posted

Sigh... I am doing something seriously WRONG and I cannot figure it out.

 

Ok, so this is what I'm doing. I have on a drawing of mine a series of objects. I highlight all of these and use WBLOCK to save them as a block dwg. Now, on this block are two arrows (you know the ones that have an arrow and circle). I give this block dynamic properties by going in the block editor and applying the rotation parameter on the center of these arrows with a varying radius (believe me I've tried a ton of different combinations.) Ok so the problem, once I run the program (same script I posted before), most of the block appears fine. Except, either one or both of the arrows appear waaaaaay of the screen somewhere. I say one or both because it depends on the way I create the block in the first place. Every time I start from scratch something different happens. I'm pulling my hair out. I must be doing something so obviously wrong but I can't figure it out. The sample clock drawing works perfect!

 

~tiger1337

Posted

post your block and somebody will have a look at it

Posted

what seems to be happening is that not only are the arrows getting rotated about them selves, but also about some other point in the drawing.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...