// The object selection functions
// This selects objects in a box
// (We don't select construction objects)
void CTinyCadDoc::Select(CDPoint p1,CDPoint p2)
{
  double left=min(p1.x,p2.x);
  double right=max(p1.x,p2.x);
  double top=min(p1.y,p2.y);
  double bottom=max(p1.y,p2.y);

  UnSelect();

	drawingIterator it = GetDrawingBegin();
	while (it != GetDrawingEnd()) 
	{
		CDrawingObject *obj = *it;

		if (    obj->IsInside(left,right,top,bottom) 
			&& !obj->IsConstruction()
			&& 
			(obj->GetType() != xJunction || !GetOption().GetAutoJunc() ) )
		{
			obj->Display();
			Select( obj );
		} 
  		
		++ it;
	}
}
示例#2
0
void CTinyCadView::OnDraw(CDC* pDC)
{
	//CTinyCadDoc* pDoc = GetCurrentDocument();
	CDC BitmapDC;
	CBitmap *old_bitmap = NULL;

	int selected;

	CRect client;
	if (pDC->IsKindOf(RUNTIME_CLASS(CPaintDC)))
	{
		client = static_cast<CPaintDC*> (pDC)->m_ps.rcPaint;
	}
	else
	{
		GetClientRect(&client);
	}

	// Are we going to use off-screen drawing?
	BOOL osb = !pDC->IsPrinting() && m_use_offscreen_drawing && CreateBitmap(*pDC, client.Width(), client.Height());

	if (osb)
	{
		BitmapDC.CreateCompatibleDC(pDC);
		old_bitmap = BitmapDC.SelectObject(&m_bitmap);
	}

	{
		CContext dc(osb ? &BitmapDC : pDC, GetTransform(), this);

		CDPoint origin = GetTransform().GetOrigin();

		if (osb)
		{
			CPoint point = CPoint(-client.left, -client.top);
			dc.SetPixelOffset(point);
		}

		if (pDC->IsPrinting())
		{
			dc.SetBlack(CTinyCadRegistry::GetPrintBandW());
		}

		CDPoint Start, End;
		CRect rect;
		GetClientRect(&rect);
		TransformSnap snap;
		snap.SetGridSnap(FALSE);
		Start = GetTransform().DeScale(snap, CPoint(rect.left, rect.top));
		End = GetTransform().DeScale(snap, CPoint(rect.right, rect.bottom));

		// Is any of this region in the off-page area?
		if (!pDC->IsPrinting())
		{

			// Paint the region white
			if (pDC->IsPrinting())
			{
				dc.SelectBrush(cWHITE);
				dc.SelectPen(PS_SOLID, 1, cWHITE);
			}
			else
			{
				COLORREF col = GetCurrentDocument()->GetOptions()->GetUserColor().Get(CUserColor::BACKGROUND);
				dc.SelectBrush(col, 0);
				dc.SelectPen(PS_SOLID, 1, col);
			}
			dc.Rectangle(CDRect(Start.x - 2, Start.y - 2, End.x + 2, End.y + 2));

			dc.SelectBrush(cOFFPAGE);
			dc.SelectPen(PS_SOLID, 1, cOFFPAGE);

			if (End.x > GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x)
			{
				CDPoint a = CDPoint(GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x, 0);
				dc.Rectangle(CDRect(a.x, a.y, End.x, End.y));
			}
			if (End.y > GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y)
			{
				CDPoint a = CDPoint(Start.x, GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y);
				dc.Rectangle(CDRect(a.x, a.y, End.x, End.y));
			}
			if (Start.x < 0) dc.Rectangle(CDRect(0, Start.y, Start.x, End.y));
			if (Start.y < 0) dc.Rectangle(CDRect(Start.x, 0, End.x, Start.y));

			// Fill this region with a grid
			double grid = GetCurrentDocument()->m_snap.GetGrid();
			double SGrid = dc.GetTransform().doubleScale(grid);
			if (GetCurrentDocument()->GetOptions()->ShowGrid() && SGrid > 10)
			{
				double x = dc.GetTransform().GetOrigin().x;
				double y = dc.GetTransform().GetOrigin().y;

				TransformSnap s = GetCurrentDocument()->m_snap;
				s.SetGridSnap(TRUE);

				x = s.Snap(x);
				y = s.Snap(y);

				for (double xp = x >= 0 ? x : 0; xp < End.x && xp < GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().x; xp += grid)
				{
					for (double yp = y >= 0 ? y : 0; yp < End.y && yp < GetCurrentDocument()->GetDetails().GetPageBoundsAsPoint().y; yp += grid)
					{
						dc.SetPixel(CDPoint(xp, yp), 0);
					}
				}
			}
		}

		Start -= CDPoint(10, 10);
		End += CDPoint(10, 10);

		GetCurrentDocument()->GetSelectBegin();

		drawingIterator it = GetCurrentDocument()->GetDrawingBegin();
		while (it != GetCurrentDocument()->GetDrawingEnd())
		{
			CDrawingObject *obj = *it;

			selected = !pDC->IsPrinting() && GetCurrentDocument()->IsSelected(obj);
			paint_options options = selected ? draw_selected : draw_normal;

			if (!pDC->IsPrinting() || !obj->IsConstruction())
			{
				if (pDC->IsPrinting() || obj->IsInside(Start.x, End.x, Start.y, End.y))
				{
					obj->Paint(dc, options);
				}
			}

			++it;
		}

		// Now draw the selectable object, so it stands out...
		CDrawingObject *obj = GetCurrentDocument()->GetSelectable();
		if (obj != NULL && !GetCurrentDocument()->IsSelected(obj))
		{
			paint_options options = draw_selectable;
			GetCurrentDocument()->GetSelectable()->Paint(dc, options);
		}

		// If only one item is selected then just draw its handles now
		if (GetCurrentDocument()->IsSingleItemSelected())
		{
			GetCurrentDocument()->GetSingleSelectedItem()->PaintHandles(dc);
		}

		// if necessary turn back on the current object to be edited
		if (GetCurrentDocument()->GetEdit() != NULL) {
			//ATLTRACE2("TinyCadView::GetCurrentDocument->GetEdit->Paint(dc, draw_selected=%d)\n",draw_selected);
			GetCurrentDocument()->GetEdit()->Paint(dc, draw_selected);
		}

		// Draw the design details
		GetCurrentDocument()->Display(dc);
	}

	if (osb)
	{
		pDC->BitBlt(client.left, client.top, client.Width(), client.Height(), &BitmapDC, 0, 0, SRCCOPY);
		BitmapDC.SelectObject(old_bitmap);
	}
}
// Perform the work of making a netlist from a single sheet in this design...
void CNetList::MakeNetForSheet( fileCollection &imports, int file_index_id, int &file_name_index, int sheet, CTinyCadDoc *pDesign )
{
  // Get rid of any old data
  m_CurrentNet = 1;
  m_nodes.erase( m_nodes.begin(), m_nodes.end() );
  m_nets.erase( m_nets.begin(), m_nets.end() );

  // Here is some temporary data for this function
  typedef std::map<CString,int> stringCollection;
  stringCollection	Powers;
  stringCollection	Connected;

  // Search for nodes, and build the node tree
  drawingIterator it = pDesign->GetDrawingBegin();
  for (;it != pDesign->GetDrawingEnd(); ++ it ) 
  {
	CDrawingObject *ObjPtr = *it;
	stringCollection::iterator found;
	int hold;
	CDPoint tr;

	switch (ObjPtr->GetType()) 
	{
		case xHierarchicalSymbol:
			{
				CDrawHierarchicalSymbol *pSymbol = static_cast<CDrawHierarchicalSymbol*>(ObjPtr);

				// Try and stop recursion by limiting the number of imports
				if (imports.size() > 100)
				{
					AfxMessageBox( IDS_RECURSION );
					continue;
				}

				// Push back this filename into the list of extra imports
				CImportFile *f = new CImportFile;
				++ file_name_index;
				f->m_file_name_index = file_name_index;
				if (f->Load( pSymbol->GetFilename() ) )
				{
					imports.push_back( f );

					// Now search the symbol for pins to link the other symbols to
					drawingCollection method;
					pSymbol->ExtractSymbol(tr,method);

					drawingIterator it = method.begin();
					while ( it != method.end() ) 
					{
						CDrawingObject *pointer = *it;

						if (pointer->GetType()==xPinEx) 
						{
							CDrawPin *thePin = static_cast<CDrawPin*>(pointer);

							// This in effect labels the node with the new node name...
							CNetListNode n( file_name_index, sheet, thePin, thePin->GetActivePoint(pSymbol) );
							n.setLabel( thePin->GetPinName() );
							n.m_reference = pSymbol->GetRefSheet(m_prefix_references,m_prefix_import,file_index_id,sheet);
							n.m_pin = thePin->GetNumber();
							n.m_pMethod = pSymbol;
							Add(n);
						}

						++ it;
					}
				}
				else
				{
					delete f;
				}
			}
			break;
		case xMethodEx3:
			#define thePin ((CDrawPin*)pointer)
			#define theMethod ((CDrawMethod*)ObjPtr)
			{
				drawingCollection method;
				((CDrawMethod *)ObjPtr)->ExtractSymbol(tr,method);

				drawingIterator it = method.begin();
				while ( it != method.end() ) 
				{
					CDrawingObject *pointer = *it;

					if (pointer->GetType()==xPinEx && !(thePin->IsPower()) ) 
					{
						CNetListNode n( file_index_id, sheet, thePin,thePin->GetActivePoint(theMethod));
						n.m_reference = theMethod->GetRefSheet(m_prefix_references,m_prefix_import,file_index_id,sheet);
						n.m_pin = thePin->GetNumber();
						n.m_pMethod = theMethod;
						Add(n);
					}

					++ it;
				}

				// Has this symbol had it's power connected?
				if (Connected.find(theMethod->GetRefSheet(m_prefix_references,m_prefix_import,file_index_id,sheet)) == Connected.end()) 
				{
					Connected[ theMethod->GetRefSheet(m_prefix_references,m_prefix_import,file_index_id,sheet) ] = TRUE;

					drawingCollection method;
					((CDrawMethod *)ObjPtr)->ExtractSymbol(tr,method);
					drawingIterator it = method.begin();
					while (it!=method.end()) 
					{
						CDrawingObject *pointer = *it;

						if (pointer->GetType()==xPinEx && thePin->IsPower()) 
						{
							CNetListNode n(file_index_id, sheet, thePin,thePin->GetActivePoint(theMethod) );
							n.m_reference = theMethod->GetRefSheet(m_prefix_references,m_prefix_import,file_index_id,sheet);
							n.m_pin = thePin->GetNumber();
							n.m_pMethod = theMethod;


							// Look up the netlist this power belongs to
							found = Powers.find( thePin->GetPinName() );
							if (found != Powers.end())
								n.m_NetList = (*found).second;
							hold = Add(n);
							if (found == Powers.end())
								Powers[thePin->GetPinName()] = hold;
						}

						++ it;
					}
				}
			}			
			break;
		case xNoConnect:
			Add(CNetListNode(file_index_id, sheet, ObjPtr,ObjPtr->m_point_a));			
			break;
		case xJunction:
			Add(CNetListNode(file_index_id, sheet, ObjPtr,ObjPtr->m_point_a));
			break;			
		case xPower:
			{
			CNetListNode n(file_index_id, sheet, ObjPtr,ObjPtr->m_point_a);
			n.setLabel( ((CDrawPower *)ObjPtr)->GetValue() );

			// Does this power item exist?
			found = Powers.find(((CDrawPower *)ObjPtr)->GetValue());
			if (found != Powers.end())
				n.m_NetList = (*found).second;
			hold = Add(n);
			if (found == Powers.end())
				Powers[((CDrawPower *)ObjPtr)->GetValue()] = hold;
			}
			break;
		case xWire:
			{
				CNetListNode n(file_index_id, sheet, ObjPtr,ObjPtr->m_point_a);
				hold = Add(n);
			}
			{
				CNetListNode n(file_index_id, sheet, ObjPtr,ObjPtr->m_point_b);
				n.m_NetList = hold;
				Add(n);
			}
			break;
	}
  }

 

  // Search for junctions and connect together
  it = pDesign->GetDrawingBegin();
  while (it != pDesign->GetDrawingEnd()) 
  {
	CDrawingObject *ObjPtr = *it;

	// Search for junctions
	if (ObjPtr->GetType() == xJunction) 
	{
		// Find out which netlist was assigned to this point
		CDPoint a = ObjPtr->m_point_a;
		int NetNumber = m_nodes[ a ];

		// Look for wires which cross this junction
		drawingIterator search_it = pDesign->GetDrawingBegin();
		while (search_it != pDesign->GetDrawingEnd()) 
		{
			CDrawingObject *search = *search_it;

			// Find the wires
			// If the wire has an end at this junction then it is already connected
			if (search->GetType()==xWire 
			 && search->m_point_a!=a && search->m_point_b!=a)
			{
				// Is this point on this wire?
				CLineUtils l( search->m_point_a, search->m_point_b );
				double distance_along_a;

				if (l.IsPointOnLine( a, distance_along_a ))
				{
					CNetListNode n(file_index_id, sheet, NULL,search->m_point_a);
					n.m_NetList = NetNumber;
					NetNumber = Add(n);
				}
			}

			++ search_it;
		}
	}
	
	++ it;
  }

  // Search for labels and connect to their respective lines
  stringCollection labels;
  it = pDesign->GetDrawingBegin();
  while (it != pDesign->GetDrawingEnd()) 
  {
	CDrawingObject *ObjPtr = *it;

	// Search for junctions
	if (ObjPtr->GetType() == xLabelEx2) 
	{
		CDPoint a = static_cast<CDrawLabel*>(ObjPtr)->GetLabelPoint();

		// Search for a wire this label is connect to
		// Only atempt to connect to a single wire
		drawingIterator search_it = pDesign->GetDrawingBegin();
		while (search_it != pDesign->GetDrawingEnd()) 
		{
			CDrawingObject *search = *search_it;
			if (search->GetType()==xWire && search->IsInside(a.x,a.x,a.y,a.y)) 
			{
				a = search->m_point_a;
				break;
			}
			
			++ search_it;
		}

		// Look up this label
		CNetListNode n(file_index_id, sheet, ObjPtr,a);
		n.setLabel(  ((CDrawLabel *)ObjPtr)->GetValue() );

		// Has this label already been assigned a netlist?
		stringCollection::iterator found = labels.find(((CDrawLabel *)ObjPtr)->GetValue());
		if (found!=labels.end()) 
		{
			n.m_NetList = (*found).second;
			n.setLabel( _T("") );
		}

		int hold = Add(n);

		// If there was no netlist write it back...
		if (found == labels.end())
			labels[ ((CDrawLabel *)ObjPtr)->GetValue()] = hold;
	}
	
	++ it;
  }  

  // Our work with the nodes map is complete, so we can discard
  // it now...
  m_nodes.erase( m_nodes.begin(), m_nodes.end() );

}