Macro to draw rectangles
Macro to draw rectangles
(OP)
I my continuous effort to eliminate AutoCAD from daily use I need help to create a macro to do the following:
1. User selects a drawing view
2. User starts macro
3. Macro draws one rectangle around the view, offset .125" from extremities and a second rectangle offset .125" from the first rectangle. The two rectangles should be on a different layer and colored red.
The question here is how to determine xmin, ymin and xmax ymax of a selected drawing view?
1. User selects a drawing view
2. User starts macro
3. Macro draws one rectangle around the view, offset .125" from extremities and a second rectangle offset .125" from the first rectangle. The two rectangles should be on a different layer and colored red.
The question here is how to determine xmin, ymin and xmax ymax of a selected drawing view?






RE: Macro to draw rectangles
You may be able to use its output directly, or in a macro.
Helpful SW websites FAQ559-520
How to get answers to your SW questions FAQ559-1091
RE: Macro to draw rectangles
Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to draw rectangles
Helpful SW websites FAQ559-520
How to get answers to your SW questions FAQ559-1091
RE: Macro to draw rectangles
We have a laser cutting machine and a software to generate the programs for laser cuts. The software accepts dxfs as input but, to generate the programs correctly, the actual contour (drawn in white) has to be enclosed by two extra contours drawn in red. Please don't ask me why!
As of now, we export SW flat patterns to dwg, add the two boxes in AutoCAD and save as dxfs.
I found this in SW API help: View.GetOutline(). It might do the trick.
RE: Macro to draw rectangles
I'm not a fan of creating in SW, then use it in ACAD.
Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to draw rectangles
RE: Macro to draw rectangles
Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to draw rectangles
I think the View.GetOutline will not work. I may need to actually select the points in the view.That would be ok. The question is how to determine X and Y for a selected vertex in a drawing view.
RE: Macro to draw rectangles
CODE
Set SelMgr = Part.SelectionManager
NumSelects = SelMgr.GetSelectedObjectCount
Set SelectedObj = SelMgr.GetSelectedObject(NumSelects)
PickPoint = SelMgr.GetSelectionPoint(NumSelects)
If SelMgr.GetSelectedObjectType2(NumSelects) > 3 Then
StickPoint = PickPoint
Else
StickPoint = SelectedObj.GetClosestPointOn(PickPoint(0), PickPoint(1), PickPoint(2))
End If
RE: Macro to draw rectangles
Chris
Systems Analyst, I.S.
SolidWorks/PDMWorks 05
AutoCAD 06
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to draw rectangles
What is your current program called?
Helpful SW websites FAQ559-520
How to get answers to your SW questions FAQ559-1091
RE: Macro to draw rectangles
handleman, does the code work for drawing views?
RE: Macro to draw rectangles
Grant
Applications Engineer
SW2006 SP 3.4
IBM InteliStation Pro M
P4 3.4 GHz, 2GB RAM
XP Pro SP2.0
NIVIDA Quadro FX 1100
RE: Macro to draw rectangles
Long story short, these functions work in drawings.
RE: Macro to draw rectangles
It will draw a minimum bounding box on your part. After using it, on your drawing, you would have to show the sketch, then offset it 1/8" twice. If you already have ACAD installed on your computer, I would use it instead. You might even find an AutoLISP program written to do what you want.
Flores
SW06 SP3.0
RE: Macro to draw rectangles
The following code will do it. The offset value is in meters (SW API works in metric units) and is given as a constant that you can change if required. You will need to create your red layer in the drawing template and have that layer active when you run the macro.
Two lines are commented out - they have the ' at the beginning of the line. If you uncomment these lines you will get the rectangles around all the drawing views. Having the comments in there stops the loop at the first drawing view.
If this was my routine, I would also put the dxf export in the code so you don't have to do it manually. It should not be difficult for you to add that in.
CODE
Const OFFSET = 0.00317
Sub Main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim vOutline As Variant
Dim vPos As Variant
Dim i As Long
Dim lViewNumber As Long
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
If swView Is Nothing Then
End
Else
lViewNumber = 1
swModel.SetAddToDB (True)
End If
' Do While Not swView Is Nothing
vOutline = swView.GetOutline
vPos = swView.Position
Debug.Print "View = " + swView.Name
Debug.Print " Pos = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"
Debug.Print " Min = (" & vOutline(0) * 1000# & ", " & vOutline(1) * 1000# & ") mm"
Debug.Print " Max = (" & vOutline(2) * 1000# & ", " & vOutline(3) * 1000# & ") mm"
Debug.Print
Set swView = swDraw.GetFirstView
swModel.SketchRectangle vOutline(0) - OFFSET, vOutline(3) + OFFSET, 0, _
vOutline(2) + OFFSET, vOutline(1) - OFFSET, 0, True
swModel.SketchRectangle vOutline(0) - 2 * OFFSET, vOutline(3) + 2 * OFFSET, 0, _
vOutline(2) + 2 * OFFSET, vOutline(1) - 2 * OFFSET, 0, True
lViewNumber = lViewNumber + 1
For i = 1 To lViewNumber
Set swView = swView.GetNextView
Next i
' Loop
With swModel
.ClearSelection2 (True)
.SetAddToDB (False)
.EditRebuild3
End With
End Sub
RE: Macro to draw rectangles
RE: Macro to draw rectangles
RE: Macro to draw rectangles
RE: Macro to draw rectangles
RE: Macro to draw rectangles
RE: Macro to draw rectangles
swDraw.ActivateView(swView.GetName2)
to activate the current view and translated the coordinates to draw the rectangles in the current view. It works fine when scale is 1:1. At scale 1:4 the rectangles are in the right position but they are 4 times smaller.
The dxf's will be 1:1 but in SW I have to fit them in the title block. In SW you can't scale the titleblock. I am sure there must be a way to draw the rectangles considering the scale effect.
One remark. The outline is not exactly the min and max of my view. In my test the outline was already 0.34" offset from the contour so in the end I may not use it and try again by selecting points.
RE: Macro to draw rectangles
RE: Macro to draw rectangles
Yes the rectangles are offset from the drawing view border, which is itself offset from the actual edges of the part. You can adjust the offset value as you require to get the offset you want from part edges to sketched rectangles. Calculating the actual position of the part edges is a more involved process than is likely required.
Do not select the view prior to running.
CODE
Const OFFSET = 0.00317
Sub Main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim vOutline As Variant
Dim vPos As Variant
Dim i As Long
Dim lViewNumber As Long
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
If swView Is Nothing Then
End
Else
swModel.EditRebuild3
lViewNumber = 1
swModel.SetAddToDB (True)
End If
' Do While Not swView Is Nothing
vOutline = swView.GetOutline
vPos = swView.Position
Debug.Print "View = " + swView.Name
Debug.Print " Pos = (" & vPos(0) * 1000# & ", " & vPos(1) * 1000# & ") mm"
Debug.Print " Min = (" & vOutline(0) * 1000# & ", " & vOutline(1) * 1000# & ") mm"
Debug.Print " Max = (" & vOutline(2) * 1000# & ", " & vOutline(3) * 1000# & ") mm"
Debug.Print
Set swView = swDraw.GetFirstView
swModel.SketchRectangle (vOutline(0) - OFFSET) / swView.ScaleDecimal, _
(vOutline(3) + OFFSET) / swView.ScaleDecimal, 0, _
(vOutline(2) + OFFSET) / swView.ScaleDecimal, _
(vOutline(1) - OFFSET) / swView.ScaleDecimal, 0, True
swModel.SketchRectangle (vOutline(0) - 2 * OFFSET) / swView.ScaleDecimal, _
(vOutline(3) + 2 * OFFSET) / swView.ScaleDecimal, 0, _
(vOutline(2) + 2 * OFFSET) / swView.ScaleDecimal, _
(vOutline(1) - 2 * OFFSET) / swView.ScaleDecimal, 0, True
lViewNumber = lViewNumber + 1
For i = 1 To lViewNumber
Set swView = swView.GetNextView
Next i
' Loop
With swModel
.ClearSelection2 (True)
.SetAddToDB (False)
.EditRebuild3
End With
End Sub
RE: Macro to draw rectangles
RE: Macro to draw rectangles